简体   繁体   中英

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.

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. i want to add to the saved bitmap also the drawn FillPie.

I think your best bet would be to use myPieGraphic.DrawImage followed by your myPieGraphic.FillPie, the use the myPieGraphic.Save method.

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?

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

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);
    }
}

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