简体   繁体   中英

How to save drawing Graphics to bitmap?

I want to save changes which i made with Graphics to Bitmap file. I have image from camera in pictureBox, and when i have mouse click, i add grid and point to image (in front of image, as i understand), after this i want to save drawing Graphics with img from camera, but i take only img without drawing grid and points. How should i do it? I create Graphics like this:

 g = Graphics.FromHwnd(postureImg.Handle);
 SolidBrush brush_Grey = new SolidBrush(Color.Black);
 SolidBrush brush_Gold = new SolidBrush(Color.Gold);                   
 Rectangle rect = new Rectangle(dPoint1, new Size(10, 10));
 g.FillEllipse(brush_Gold, rect);                   

 points[i] = new Point(e.X, e.Y);
       i++;
       if (i >= 2)
       {
        Pen myPen = new Pen(Color.Red);
        myPen.Width = 1;
        g.DrawLine(myPen, points[0].X, points[0].Y, points[1].X, points[1].Y);
        }
        g.Dispose();

Next time i make g.DrawLine and g.FillEllipse and have image like this: 在此处输入图片说明 How can i take this image to bitmap? Thx for help!

Okay, a quick tutorial on what you're doing.

First up, the Graphics object? All it's doing is modifying the raw image/bitmap that you're pointing it to. In this case, you're modifying the raw image/bitmap that is contained in your postureImg. That's why you're not having to 'reimport' the picture back into that pictureBox - because the graphics is modifying it in-place.

That means that, afterwards, all you have to do is save that raw image/bitmap to file - so what you're really asking is: "How do I save a bitmap that's in a PictureBox to a file?"

In which case, the answer's pretty straight-forward:

postureImg.Image.Save(@"C:\someplace.jpg", ImageFormat.Jpeg);

EDIT: Ah, I forgot that VS does some wonky stuff with PicBoxes - it has an 'actual' image and a 'displayed' image. What you've been editing is the 'displayed' image, which isn't persistent (it'll go away if the form refreshes.)

To be honest, you'll probably be better off if you never go straight from the image in the picture box. For instance, here's code that doesn't work:

Graphics g = Graphics.FromHwnd(pictureBox1.Handle);
SolidBrush brush_Grey = new SolidBrush(Color.Green);
SolidBrush brush_Gold = new SolidBrush(Color.Red);
Rectangle rect = new Rectangle(new Point(100, 100), new Size(10, 10));
g.FillEllipse(brush_Gold, rect);
g.Dispose();
pictureBox1.Image.Save(@"C:\tmpSO1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

... a good clue that it won't work is, if you perform this on form load it won't display the red circle; and if the form has to refresh due to clipping or such, the red circle will go away.

Anyway, here's code that does work:

Bitmap bmp = new Bitmap(pictureBox1.Image);
Graphics g = Graphics.FromImage(bmp);
SolidBrush brush_Grey = new SolidBrush(Color.Green);
SolidBrush brush_Gold = new SolidBrush(Color.Red);
Rectangle rect = new Rectangle(new Point(100, 100), new Size(10, 10));
g.FillEllipse(brush_Gold, rect);
g.Dispose();
pictureBox1.Image = bmp;
pictureBox1.Image.Save(@"C:\tmpSO2.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

Instead of modifying the PictureBox in-place, a separate BMP is loaded from it, and back into it.

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