简体   繁体   中英

Insert a screenshot in Windows Form Picturebox C#

I am writed this method in my WindowsForm application to take a screenshot from a part of screen:

public static Image Preview(int startX, int startY, int width, int height)
{
    Rectangle bounds = new Rectangle(startX, startY, width, height);

    using (Bitmap scr = new Bitmap(bounds.Width, bounds.Height))
    {
        using (Graphics g = Graphics.FromImage(scr))
        {
            g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
        }
        return scr;
    }
}

Then I entered the return value in a PictureBox:

MyPictureBox.Image = Preview(0, 0, 1080, 720);

But when I start it I get this error:

System.ArgumentException: 'Parameter is not valid.'

What's the problem? (I apologize for the grammatical mistakes, I am not a native English speaker)

I no longer get the error if I remove the using :

        Bitmap scr = new Bitmap(bounds.Width, bounds.Height);
        {
            using (Graphics g = Graphics.FromImage(scr))
            {
                g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
            }
        }
        return scr;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM