Why the pie is not rotating in the center of the pictureBox ?

Daniel Lee 61 Reputation points
2022-11-30T00:24:57.187+00:00
using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Drawing.Drawing2D;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows.Forms;  
  
namespace My_Pie  
{  
    public partial class Form1 : Form  
    {  
        int myPiePercent = 15;  
  
        public Form1()  
        {  
            InitializeComponent();  
  
            timer1.Enabled = true;  
        }  
  
        private void pictureBox1_Paint(object sender, PaintEventArgs e)  
        {  
            e.Graphics.DrawRectangle(Pens.Green, 0, 0, pictureBox1.Width - 1, pictureBox1.Height - 1);  
  
            DrawPieOnPicturebox(e.Graphics);  
        }  
  
        public void DrawPieOnPicturebox(Graphics myPieGraphic)  
        {  
            Color myPieColors = Color.LightGreen;  
            Point myPieLocation = new Point(pictureBox1.ClientRectangle.Width / 2,  
                pictureBox1.ClientRectangle.Height / 2);  
            Size myPieSize = new Size(150, 150);  
            DrawMyPie(myPiePercent, myPieColors, myPieGraphic, myPieLocation, myPieSize);  
        }  
  
        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));  
            }  
        }  
  
        private void timer1_Tick(object sender, EventArgs e)  
        {  
            myPiePercent++;  
            pictureBox1.Invalidate();  
        }  
    }  
}  
  
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,869 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.
10,605 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 114.4K Reputation points
    2022-11-30T09:22:51.807+00:00

    Check some modifications:

    public void DrawPieOnPicturebox( Graphics myPieGraphic )  
    {  
        Color myPieColors = Color.LightGreen;  
        Size myPieSize = new Size( 150, 150 );  
        Point myPieLocation = new Point( ( pictureBox1.ClientRectangle.Width - myPieSize.Width ) / 2, ( pictureBox1.ClientRectangle.Height - myPieSize.Height ) / 2 );  
        DrawMyPie( myPiePercent, myPieColors, myPieGraphic, myPieLocation, myPieSize );  
    }  
    
    0 comments No comments

0 additional answers

Sort by: Most helpful