简体   繁体   中英

How can i save graphics.copyfromscreen to a bitmap file on hard disk?

In a button click event:

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            painting = false;
            pictureBox2.Invalidate();
        }

Then in the paint event:

private void pictureBox2_Paint(object sender, PaintEventArgs e)
        {
            Point pnt;

            if (rect.Width > 10 && rect.Height > 10)
            {
                pnt = PointToScreen(pictureBox1.Location);
                e.Graphics.Clear(Color.White);
                e.Graphics.CopyFromScreen(pnt.X + rect.X, pnt.Y + rect.Y, rect.X, rect.Y, new Size(rect.Width, rect.Height));
            }
        }

I want to save to a bitmap file not the whole pictureBox2.Image but only the CopyFromScreen part.

This is a screenshot of pictureBox2

屏幕截图

The pictureBox2 in the screenshot borders are also the white color around the image. But I want to save only the image not the whole pictureBox2.Image and to do it before the pictureBox2 paint event maybe in the button click event. So the image only will be saved to a bitmap.

If all what you want is to save something from the Graphics object (whether you have used the CopyFromScreen call or not), you can refer to something that have been posted here before: Saving System.Drawing.Graphics to a png or bmp

Or even there: How does the Graphics CopyFromScreen method copy into a bitmap?

Please note that you don't necessarily need to use the PictureBox Paint event handler for that.

Let me know whether this answers your question =]

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