Snipping tool like app w source code and exe

Discussion in 'Programming General' started by hampe-92, Mar 26, 2009.

Snipping tool like app w source code and exe
  1. Unread #1 - Mar 26, 2009 at 4:48 PM
  2. hampe-92
    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0

    hampe-92 Forum Addict

    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"valueMicrosoft.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", (sendere) => { SetPath(); });
                
    icon.ContextMenu.MenuItems.Add("Open Folder In Explorer", (sendere) => { System.Diagnostics.Process.Start(Path); });
                
    MenuItem cToCboard = new MenuItem("Copy Image To Clipboard");
                
    cToCboard.Click += (sendere) => { if (copyToClipboard) { cToCboard.Checked falsecopyToClipboard false; } else { cToCboard.Checked truecopyToClipboard true; } };
                
    cToCboard.Checked true;
                
    icon.ContextMenu.MenuItems.Add(cToCboard);
                
    icon.ContextMenu.MenuItems.Add("Remove Registry Key", (sendere) => { try { Microsoft.Win32.Registry.CurrentUser.DeleteSubKey(@"Software\Rapid Shot"); } catch (Exception ex) { } });
                
    icon.ContextMenu.MenuItems.Add("About", (sendere) => { 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", (sendere) => { Application.Exit(); });
                
    icon.DoubleClick += (sendere) => { 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 += (sendere) =>
                {
                    
    mousePos = (down) ? Cursor.Position upPos;
                    
    selectedRect = new Rectangle(Math.Min(downPos.XmousePos.X), Math.Min(downPos.YmousePos.Y), Math.Abs(downPos.mousePos.X), Math.Abs(downPos.mousePos.Y));
                    
    e.Graphics.FillRectangle(new SolidBrush(Color.White), selectedRect);
                    
    e.Graphics.DrawRectangle(new Pen(Color.FromArgb(0xFF0x000x00), 1), selectedRect);
                    
    e.Graphics.DrawString("Esc - Exit\nEnter - Save selected", new Font("Arial"12), new SolidBrush(Color.Black), new PointF(((float)mousePos.10), ((float)mousePos.10)));
                };
                
    SelectFrm.MouseDown += (sendere) => { down truedownPos = new Point(e.Xe.Y); };
                
    SelectFrm.MouseMove += (sendere) => { if (downSelectFrm.Invalidate(); };
                
    SelectFrm.MouseUp += (sendere) => { down falseupPos = new Point(e.Xe.Y); };

                
    SelectFrm.KeyDown += (sendere) =>
                {
                    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.WidthScreen.PrimaryScreen.Bounds.HeightSystem.Drawing.Imaging.PixelFormat.Format32bppArgb);
                        var 
    snapShot Graphics.FromImage(bitmap);
                        
    snapShot.CopyFromScreen(00Screen.PrimaryScreen.Bounds.XScreen.PrimaryScreen.Bounds.YScreen.PrimaryScreen.Bounds.SizeCopyPixelOperation.SourceCopy);
                        
    bitmap bitmap.Clone(selectedRectPixelFormat.Format32bppArgb);
                        
    bitmap.Save(String.Format("{0}\\Screenshots {1}.png"PathDateTime.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 :)
     
  3. Unread #2 - Apr 1, 2009 at 3:38 PM
  4. hampe-92
    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0

    hampe-92 Forum Addict

    Snipping tool like app w source code and exe

    Isn't there anyone here on sythe that is interested in C#???
     
  5. Unread #3 - Apr 8, 2009 at 2:51 PM
  6. arturconfituur
    Joined:
    Apr 8, 2009
    Posts:
    1
    Referrals:
    0
    Sythe Gold:
    0

    arturconfituur Newcomer

    Snipping tool like app w source code and exe

    i do

    im learning it
     
  7. Unread #4 - Apr 8, 2009 at 5:29 PM
  8. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    Snipping tool like app w source code and exe

    The programming section is small - hence there isn't much publicity.
     
  9. Unread #5 - Apr 15, 2009 at 2:06 PM
  10. Under3
    Joined:
    Apr 14, 2009
    Posts:
    10
    Referrals:
    0
    Sythe Gold:
    0

    Under3 Newcomer

    Snipping tool like app w source code and exe

    Nice. I've got a handful of codes as well.
     
  11. Unread #6 - Apr 15, 2009 at 2:15 PM
  12. hampe-92
    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0

    hampe-92 Forum Addict

    Snipping tool like app w source code and exe

    thx.. mind posting yours if they are something to share? :p
     
  13. Unread #7 - Apr 26, 2009 at 4:25 AM
  14. ThE_ViKinG
    Joined:
    Mar 4, 2006
    Posts:
    2
    Referrals:
    0
    Sythe Gold:
    0

    ThE_ViKinG Newcomer

    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
     
  15. Unread #8 - Apr 26, 2009 at 9:13 AM
  16. hampe-92
    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0

    hampe-92 Forum Addict

    Snipping tool like app w source code and exe

    Why would I want someone else's source?
     
< Need help. | In need of a vb6 tool >

Users viewing this thread
1 guest


 
 
Adblock breaks this site