How to save a graphics fillpie to bitmap image fileo n the hard disk ?

Daniel Lee 61 Reputation points
2022-12-01T07:59:57.043+00:00
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++;  
        }  

This save only the pictureBox1.Image but not the FillPie graphics.
I want to include when saving also the FillPie graphics.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,908 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,114 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,611 Reputation points Microsoft Vendor
    2022-12-02T08:22:02.613+00:00

    @Daniel Lee , Welcome to Microsoft Q&A, I suggest that you could use the Clone method to overwrite an instance of your picturebox's image with a subspace of the image.

    Here is a code example you could refer to.

     private void timer1_Tick(object sender, EventArgs e)  
            {  
                Size myPieSize = new Size(150, 150);  
                Point myPieLocation = new Point((pictureBox1.ClientRectangle.Width - myPieSize.Width) / 2, (pictureBox1.ClientRectangle.Height - myPieSize.Height) / 2);  
                Bitmap varBmp = new Bitmap(pictureBox1.Width,pictureBox1.Height);  
                pictureBox1.DrawToBitmap(varBmp, new Rectangle(new Point(0,0), pictureBox1.Size));  
                varBmp = varBmp.Clone(new RectangleF(myPieLocation, myPieSize), varBmp.PixelFormat);  
                varBmp.Save(@"e:\Test\" + counter + "mybitmap1.bmp");  
                myPiePercent++;  
                pictureBox1.Invalidate();  
                counter++;  
            }  
    

    Tested result:

    266504-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.