{Source} C# Simple Screen Snapping Program.

InfernoMods Dec 12, 2013

  1. InfernoMods

    InfernoMods Newbie BANNED
    0/47

    Joined:
    Dec 12, 2013
    Messages:
    35
    Likes Received:
    4
    Trophy Points:
    0
    Gender:
    Male
    Location:
    Hell Michigan
    Console:
    Xbox
    This is NOT my tutorial, I am simply sharing this with the public:​
    Just make a new project, and replace the code in Program.cs.​



    Code:
    using System;
    using System.Windows.Forms;
    using System.Drawing;
    using System.Drawing.Imaging;
     
    namespace screen_snipping
    {
        static class Program
        {
            private static Image fullDesktop;
            private static Image partIamge;
            private static Form backgroundForm;
            private static PictureBox picbox1;
     
            private static Point mouseDownAt;
            private static Point mouseIsAt;
            private static bool isMouseDown = false;
     
            private static Color backgroundColor = Color.DarkGray;
            private static int backgroundAlpha = 150;
            private static Color outlineColor = Color.Red;
            private static int outlineWidth = 1;
     
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
     
                fullDesktop = CaptureScreen;
                Initialize();
     
                Application.Run(backgroundForm);
            }
     
            private static void Initialize()
            {
                backgroundForm = new Form();
                backgroundForm.FormBorderStyle = FormBorderStyle.None;
                backgroundForm.WindowState = FormWindowState.Maximized;
                backgroundForm.Cursor = Cursors.Cross;
     
                picbox1 = new PictureBox();
                picbox1.Size = Screen.FromControl(backgroundForm).Bounds.Size;
                picbox1.Location = new Point(0, 0);
                picbox1.Image = fullDesktop;
                picbox1.BorderStyle = BorderStyle.None;
                picbox1.Paint += new PaintEventHandler(OnPaint);
                picbox1.MouseDown += new MouseEventHandler(OnMouseDown);
                picbox1.MouseMove += new MouseEventHandler(OnMouseMove);
                picbox1.MouseUp += new MouseEventHandler(OnMouseUp);
                backgroundForm.Controls.Add(picbox1);
            }
     
            private static void OnPaint(object sender, PaintEventArgs e)
            {
                SolidBrush opaqueWhiteBrush = new SolidBrush(Color.FromArgb(backgroundAlpha, backgroundColor.R, backgroundColor.G, backgroundColor.B));
                e.Graphics.FillRectangle(opaqueWhiteBrush, 0, 0, picbox1.Width, picbox1.Height);
     
                if (isMouseDown)
                {
                    Rectangle pos = getMouseMoveRect;
                    e.Graphics.DrawRectangle(new Pen(outlineColor, outlineWidth), new Rectangle(pos.X - outlineWidth, pos.Y - outlineWidth, pos.Width + outlineWidth, pos.Height + outlineWidth));
                    e.Graphics.DrawImage(partIamge, pos.Location);
     
                    string displayText = "W: " + pos.Width + " H: " + pos.Height;
                    Font font = new Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Pixel);
                    e.Graphics.DrawString(displayText, font, Brushes.White, pos.X, pos.Y - font.Size - 6, StringFormat.GenericDefault);
                }
            }
     
            private static void OnMouseDown(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Right && !isMouseDown)
                {
                    Application.Exit();
                }
                else if (e.Button == MouseButtons.Left)
                {
                    mouseDownAt = e.Location;
                    Cursor.Position = new Point(e.Location.X + 1, e.Location.Y + 1);
                    mouseIsAt = new Point(e.Location.X + 1, e.Location.Y + 1);
                    partIamge = ((Bitmap)fullDesktop).Clone(getMouseMoveRect, fullDesktop.PixelFormat);
                    isMouseDown = true;
                    picbox1.Refresh();
                }
            }
     
            private static void OnMouseMove(object sender, MouseEventArgs e)
            {
                if (isMouseDown)
                {
                    mouseIsAt = e.Location;
                    Rectangle rect = getMouseMoveRect;
                    if (rect.Width != 0 && rect.Height != 0)
                    {
                        partIamge = ((Bitmap)fullDesktop).Clone(rect, fullDesktop.PixelFormat);
                        picbox1.Refresh();
                    }
                }
            }
     
            private static void OnMouseUp(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    bool hasMoved = false;
     
                    if (isMouseDown)
                    {
                        hasMoved = true;
                        isMouseDown = false;
                    }
     
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "PNG image (*.png)|*.png";
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        partIamge.Save(sfd.FileName, ImageFormat.Png);
                    }
                    else
                    {
                        hasMoved = false;
                        picbox1.Refresh();
                    }
     
                    if (hasMoved)
                        Application.Exit();
                }
            }
     
            private static Rectangle getMouseMoveRect
            {
                get
                {
                    int x = 0;
                    int y = 0;
                    int width = 0;
                    int height = 0;
     
                    if (mouseIsAt.X > mouseDownAt.X)
                    {
                        x = mouseDownAt.X;
                        width = mouseIsAt.X - mouseDownAt.X;
                    }
                    else
                    {
                        x = mouseIsAt.X;
                        width = mouseDownAt.X - mouseIsAt.X;
                    }
     
                    if (mouseIsAt.Y > mouseDownAt.Y)
                    {
                        y = mouseDownAt.Y;
                        height = mouseIsAt.Y - mouseDownAt.Y;
                    }
                    else
                    {
                        y = mouseIsAt.Y;
                        height = mouseDownAt.Y - mouseIsAt.Y;
                    }
     
                    return new Rectangle(x, y, width, height);
                }
            }
     
            private static Image CaptureScreen
            {
                get
                {
                    Bitmap image = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
                    using (Graphics g = Graphics.FromImage(image))
                    {
                        g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                                 Screen.PrimaryScreen.Bounds.Y,
                                 0, 0,
                                 image.Size,
                                 CopyPixelOperation.SourceCopy);
                    }
     
                    return image;
                }
            }
        }
    }
     
  2. Heistful

    Heistful Banned (Read The Rules) BANNED
    0/47

    Joined:
    Nov 8, 2013
    Messages:
    131
    Likes Received:
    22
    Trophy Points:
    0
    Console:
    Xbox
    Nice tutorial man, i saw that nobody gave you any feedback for at least sharing it... good work!
     

Share This Page

Close