3

How can I capture a screenshot giving two points to It and basically create a rectangle screenshot or something?

I used this: How to create a RectangleF using two PointF?

But It doesn't seem to be getting the rectangle screenshot I want, It takes me the corner of the screen.

    private void KListener_KeyDown(object sender, RawKeyEventArgs args)
    {
        if (args.Key.ToString() == "F5")
        {
            Program.FirstPos = System.Windows.Forms.Cursor.Position;
            System.Media.SystemSounds.Asterisk.Play();
        }
        else if (args.Key.ToString() == "F6")
        {
            Program.SecondPos = System.Windows.Forms.Cursor.Position;
            System.Media.SystemSounds.Asterisk.Play();
        }
    }

    public Bitmap CaptureScreen()
    {
        RectangleF rect2 = GetRectangle(Program.FirstPos, Program.SecondPos);
        var image = new Bitmap((int)rect2.Width, (int)rect2.Height, PixelFormat.Format24bppRgb);
        var gfx = Graphics.FromImage(image);
        gfx.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
        image.Save("C:\\Users\\DreTaX\\Documents\\teszt", ImageFormat.Jpeg);
        return image;
    }
Community
  • 1
  • 1
DreTaX
  • 760
  • 2
  • 9
  • 22
  • Did you account for the coordinates being based on top left corner of the screen. Also, showing us what you have written so far will increase your chance of getting help. – Jacobr365 Jan 31 '16 at 17:01
  • There you go. All I do Is save two points (Which are different, I tested It), then use the method to make an image of that. – DreTaX Jan 31 '16 at 17:04
  • don't think this will change anything but you image variable is a bitmap so it can be 'Bitmap image = ...' and gfx is of type Graphics so it can be 'Graphics gfx = ...' no reason to use var when you run your code. what do you get for values of the rectangle positions when you set rect2? – Jacobr365 Jan 31 '16 at 17:10
  • gfx.CopyFromScreen(rect2.Left, rect2.Top, 0, 0, image.Size, CopyPixelOperation.SourceCopy); see here http://stackoverflow.com/questions/3306600/c-how-to-take-a-screenshot-of-a-portion-of-screen – Jacobr365 Jan 31 '16 at 17:21
  • @Jacobr365 It doesn't matter at all wheater I use builtin types or not. I will check that one our in a second. Btw this is the image I get: https://db.tt/GHmyfJ0S So the Second position is perfect but somehow the first coordinate gets reversed. It's reflecting – DreTaX Jan 31 '16 at 17:29
  • The line works, cheers! Push It as an answer. – DreTaX Jan 31 '16 at 17:31

1 Answers1

1

gfx.CopyFromScreen(rect2.Left, rect2.Top, 0, 0, image.Size, CopyPixelOperation.SourceCopy);

Jacobr365
  • 846
  • 11
  • 24