简体   繁体   English

如何将绘图图形保存到位图?

[英]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. 我在pictureBox中有来自相机的图像,当我单击鼠标时,我添加了网格并指向图像(据我所知,在图像的前面),在此之后,我想从相机中保存带有img的图形,但我只img,不绘制网格和点。 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: 下次我做g.DrawLine和g.FillEllipse并有这样的图像: 在此处输入图片说明 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? 首先,Graphics对象? 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. 在这种情况下,您要修改您的poseImg中包含的原始图像/位图。 That's why you're not having to 'reimport' the picture back into that pictureBox - because the graphics is modifying it in-place. 这就是为什么您不必将图片“重新导入”到该PictureBox中的原因-因为图形就地对其进行了修改。

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?" 这意味着,之后,您要做的就是将原始图像/位图保存到文件中-因此,您真正要问的是:“如何将PictureBox中的位图保存到文件中?”

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. 编辑:啊,我忘了VS用PicBoxes做一些奇怪的事情-它有一个“实际”图像和一个“显示”图像。 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. 无需就地修改PictureBox,而是从中加载单独的BMP,然后将其重新加载到其中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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