DreTaX
DreTaX

Reputation: 836

Capture a screenshot using two points

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;
    }

Upvotes: 3

Views: 514

Answers (1)

Jacobr365
Jacobr365

Reputation: 846

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

Upvotes: 1

Related Questions