简体   繁体   English

如何保存图片框中的图像,包括上面绘制的图形?

[英]How to save the image from picturebox including the drawn graphics on it?

i want to save the image in the pictureBox1 including the Graphics.我想将图像保存在 pictureBox1 中,包括图形。

private void pictureBox1_Paint(object sender, PaintEventArgs e)
         {
             e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
             e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
             e.Graphics.DrawRectangle(Pens.Green, 0, 0, pictureBox1.Width - 1, pictureBox1.Height - 1);
    
             Pen p = new Pen(Color.Red);
             e.Graphics.DrawLine(p, 256, 0, 256, 512);
             e.Graphics.DrawLine(p, 0, 256, 512, 256);
    
             DrawPieOnPicturebox(e.Graphics);
         }
    
         public void DrawPieOnPicturebox(Graphics myPieGraphic)
         {
             Color myPieColors = Color.FromArgb(150, Color.LightGreen);
             Size myPieSize = new Size((int)distanceFromCenterPixels, (int)distanceFromCenterPixels);
             Point myPieLocation = new Point((pictureBox1.Width - myPieSize.Width) / 2, (pictureBox1.Height - myPieSize.Height) / 2);
             DrawMyPie(myPiePercent, myPieColors, myPieGraphic, myPieLocation, myPieSize);
         }
    
         int counter = 0;
         public void DrawMyPie(int myPiePerecent, Color myPieColor, Graphics myPieGraphic, Point
       myPieLocation, Size myPieSize)
         {
             using (SolidBrush brush = new SolidBrush(myPieColor))
             {
                 myPieGraphic.FillPie(brush, new Rectangle(myPieLocation, myPieSize), Convert.ToSingle(myPiePerecent * 360 / 100), Convert.ToSingle(15 * 360 / 100));
             }
    
             myBitmap = new Bitmap(pictureBox1.Image);
             myBitmap.Save(@"d:\" + counter + "mybitmap1.bmp");
             myBitmap.Dispose();
             counter++;
         }

it's saving the image in the pictureBox1 but without the drawn FillPie.它将图像保存在 pictureBox1 中,但没有绘制 FillPie。 i want to add to the saved bitmap also the drawn FillPie.我想将绘制的 FillPie 添加到保存的 bitmap 中。

I think your best bet would be to use myPieGraphic.DrawImage followed by your myPieGraphic.FillPie, the use the myPieGraphic.Save method.我认为最好的办法是使用 myPieGraphic.DrawImage,然后使用 myPieGraphic.FillPie,然后使用 myPieGraphic.Save 方法。

It's unclear how this is layered in your app but I'm assuming you have an Image in a Picturebox and then you draw on top of it using a Graphics?目前还不清楚这是如何在您的应用程序中分层的,但我假设您在 Picturebox 中有一个 Image,然后您使用 Graphics 在它上面绘制?

Doing that will render to separate "Layers" so to speak they are not the same Image/Graphics.这样做会渲染到单独的“层”,可以说它们不是相同的图像/图形。 If you instead draw both the Image and your Pie using the same Graphics you can then save directly from it.如果您改为使用相同的图形绘制图像和饼图,则可以直接从中保存。

I think that will produce the result you want.我认为这会产生你想要的结果。

See link for all Graphics methods: https://learn.microsoft.com/en-us/do.net/api/system.drawing.graphics.addmetafilecomment?view=do.net-plat-ext-7.0查看所有图形方法的链接: https://learn.microsoft.com/en-us/do.net/api/system.drawing.graphics.addmetafilecomment?view=do.net-plat-ext-7.0

Edit: Answering comment.编辑:回答评论。

using(MemoryStream ms = new MemoryStream()) {
    bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    using (var fs = new FileStream(@"c:\temp\test.png", FileMode.Create))
    {
        var bytes = ms.ToArray();
        await fs.WriteAsync(bytes , 0, bytes.Length);
    }
}

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

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