• Log in or sign up for an account here: https://www.sythe.org/login

Snipping tool like app w source code and exe

hampe-92

Forum Addict
Joined
Jul 10, 2008
Sythe Gold
0
Messages
328
Snipping tool like app w source code and exe
I wrote a simple Snipping tool like app that saves your image to a specified location. I attached the exe at the bottom of this post...

anyway, for those that is interested in learning C# you can learn the following (and more) by studying and playing around with the code;

*How to read/write from/to the registry
*How to use get/set
*The Lambda expression to write shorter code
*Conditional/Ternary operator ( ?: ) aka Inline if

Program features:
Select a path where you want to save your images. This path will be saved to the registry.
The ability to clean the registry from what the program have written.
Double click the tray icon to bring up a board where you can draw-select a part of the screen that you wish to take a print screen of.
Press Enter to save the selected area.
If you wish to print the whole screen, double click the icon and press enter without drawing anything.
Press escape to close without saving the selected area.
The ability to toggle weather to copy the image to the clipboard or not.

I suggest you copy the code and paste it in VS for better reading...
To use, create a new windows Forms Application project, copy the code, paste and overwrite all code in Program.cs and delete the Form1.cs . change to correct namespace in the begining...
here goes the code:
PHP:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;

namespace Rapid_Shot
{
    static class Program
    {

        private static bool down = false;
        private static bool copyToClipboard = true;
        private static Point downPos;
        private static Point mousePos;
        private static Point upPos;
        private static Rectangle selectedRect;
        private static string Path
        {
            get { return Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"Software\Rapid Shot").GetValue("Path", Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)).ToString(); }
            set { Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"Software\Rapid Shot").SetValue("Path", value, Microsoft.Win32.RegistryValueKind.String); }
        }

        [STAThread]
        static void Main()
        {
            var icon = new NotifyIcon();
            icon.Icon = Rapid_Shot.Properties.Resources.monitor;
            icon.Text = "Rapid Shot";
            icon.Visible = true;
            icon.ContextMenu = new ContextMenu();
            icon.ContextMenu.MenuItems.Add("Set Path", (sender, e) => { SetPath(); });
            icon.ContextMenu.MenuItems.Add("Open Folder In Explorer", (sender, e) => { System.Diagnostics.Process.Start(Path); });
            MenuItem cToCboard = new MenuItem("Copy Image To Clipboard");
            cToCboard.Click += (sender, e) => { if (copyToClipboard) { cToCboard.Checked = false; copyToClipboard = false; } else { cToCboard.Checked = true; copyToClipboard = true; } };
            cToCboard.Checked = true;
            icon.ContextMenu.MenuItems.Add(cToCboard);
            icon.ContextMenu.MenuItems.Add("Remove Registry Key", (sender, e) => { try { Microsoft.Win32.Registry.CurrentUser.DeleteSubKey(@"Software\Rapid Shot"); } catch (Exception ex) { } });
            icon.ContextMenu.MenuItems.Add("About", (sender, e) => { MessageBox.Show("Rapid Shot is a snipping tool (the print screen utility in vista) like app that automaticly saves you images. Double click the tray icon to draw a selection.\n Created by Hampus aka Hampe-92"); });
            icon.ContextMenu.MenuItems.Add("Exit", (sender, e) => { Application.Exit(); });
            icon.DoubleClick += (sender, e) => { Snap(); };
            Application.Run();
            icon.Dispose();
        }

        private static void Snap()
        {
            var SelectFrm = new Form();
            SelectFrm.WindowState = FormWindowState.Maximized;
            SelectFrm.FormBorderStyle = FormBorderStyle.None;
            SelectFrm.ShowInTaskbar = false;
            SelectFrm.BackColor = Color.White;
            SelectFrm.Opacity = 0.5;

            SelectFrm.Paint += (sender, e) =>
            {
                mousePos = (down) ? Cursor.Position : upPos;
                selectedRect = new Rectangle(Math.Min(downPos.X, mousePos.X), Math.Min(downPos.Y, mousePos.Y), Math.Abs(downPos.X - mousePos.X), Math.Abs(downPos.Y - mousePos.Y));
                e.Graphics.FillRectangle(new SolidBrush(Color.White), selectedRect);
                e.Graphics.DrawRectangle(new Pen(Color.FromArgb(0xFF, 0x00, 0x00), 1), selectedRect);
                e.Graphics.DrawString("Esc - Exit\nEnter - Save selected", new Font("Arial", 12), new SolidBrush(Color.Black), new PointF(((float)mousePos.X + 10), ((float)mousePos.Y + 10)));
            };
            SelectFrm.MouseDown += (sender, e) => { down = true; downPos = new Point(e.X, e.Y); };
            SelectFrm.MouseMove += (sender, e) => { if (down) SelectFrm.Invalidate(); };
            SelectFrm.MouseUp += (sender, e) => { down = false; upPos = new Point(e.X, e.Y); };

            SelectFrm.KeyDown += (sender, e) =>
            {
                if (e.KeyCode == Keys.Enter)
                    SelectFrm.DialogResult = DialogResult.OK;
                else if (e.KeyCode == Keys.Escape)
                    SelectFrm.DialogResult = DialogResult.Cancel;
            };


            if (SelectFrm.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    var bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                    var snapShot = Graphics.FromImage(bitmap);
                    snapShot.CopyFromScreen(0, 0, Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
                    bitmap = bitmap.Clone(selectedRect, PixelFormat.Format32bppArgb);
                    bitmap.Save(String.Format("{0}\\Screenshots {1}.png", Path, DateTime.Now.ToString().Replace(":", "")), ImageFormat.Png);
                    SelectFrm.Dispose();
                    if (copyToClipboard)
                        Clipboard.SetImage(bitmap);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("Error: {0}", ex.Message));
                }
            }
        }

        private static void SetPath()
        {
            var dialog = new FolderBrowserDialog();
            dialog.SelectedPath = Path;
            if (dialog.ShowDialog() == DialogResult.OK)
                Path = dialog.SelectedPath;
        }
    }
}
Feel free to ask questions :)
 
Snipping tool like app w source code and exe
Isn't there anyone here on sythe that is interested in C#???
 
Snipping tool like app w source code and exe
Isn't there anyone here on sythe that is interested in C#???
The programming section is small - hence there isn't much publicity.
 
Snipping tool like app w source code and exe
Nice. I've got a handful of codes as well.
 
Snipping tool like app w source code and exe
hampe, on cruels there are some sources laying around. Sythe is mainly a forum for leechers and people who doesnt care about programing. I wish the good forums still were alive
 
Runescape Gamblingrunescape gambling
Back
Top