Home > OS >  How to save the image from picturebox including the drawn graphics on it?
How to save the image from picturebox including the drawn graphics on it?

Time:12-01

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.

CodePudding user response:

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/dotnet/api/system.drawing.graphics.addmetafilecomment?view=dotnet-plat-ext-7.0

  • Related