RyanTimmons91 RyanTimmons91 - 28 days ago 8
C# Question

FillPie with n sections Brush Color issue

When trying to use FillPie on my graphics object, if I actually draw the entire pie it all comes out as a solid color (I'm using the print-preview object)

I've tried moving objects around to pinpoint the issue, here is what I have now:

//Each brush is made static, mostly because I wanted to make sure I wasn't mucking them up
internal static SolidBrush Red = new SolidBrush(Color.Red);
internal static SolidBrush Blue = new SolidBrush(Color.Blue);
internal static SolidBrush Green = new SolidBrush(Color.Green);
internal static SolidBrush Yellow = new SolidBrush(Color.Yellow);
internal static SolidBrush Purple = new SolidBrush(Color.Purple);
internal static SolidBrush Teal = new SolidBrush(Color.Teal);
internal static SolidBrush Gold = new SolidBrush(Color.Gold);
internal static SolidBrush Fuchsia = new SolidBrush(Color.Fuchsia);

//Add each brush to an array for easier access
SolidBrush[] brushes = new SolidBrush[] {
Red,
Blue,
Green,
Yellow,
Purple,
Teal,
Gold,
Fuchsia
};

//The outlining pen for our pie
Pen BlackPen = new Pen(Color.Black, 10);

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
//The bounding box of our Pie
Rectangle FirstPie = new Rectangle(100, 100, 400, 400);

//How many slices of pie?
float maxParts = 7;

//Get the angle for each slice
float partSize = 360.00F / maxParts;

for (int i = 0; i < maxParts; i++)
{
//Get the angles for this slice
float AngleStart = (float)i * partSize;
float AngleEnd = AngleStart + partSize;

//Check that we aren't overflowing somehow (issue persists without these lines)
if (AngleStart < 0F) AngleStart = 0F;
if (AngleEnd > 360F) AngleEnd = 360F;

//Figure out which brush we are using
int brush = (int)(i % 8);

//Output for Debug
Console.WriteLine(AngleStart + " " + AngleEnd + " " + brush);

//Draw the pie and overlay
e.Graphics.FillPie(brushes[brush], FirstPie, AngleStart, AngleEnd);
e.Graphics.DrawPie(BlackPen, FirstPie, AngleStart, AngleEnd);
}
}


I get a solid-color Pie with a line running from the upper right to the center (from the DrawPie):

Image of the solid color Pie

I can verify the angles are correct by commenting out the FillPie line and only using DrawPie:

Pie Wireframe

If you look at the array and extrapolate what is happening, the entire pie is being drawn by the last color-brush being used, Gold. I output the angles and which brush is being used to the console, it is properly cycling through the brush array:

Brush array cycling

and if I change the number of Pie parts to a different number the final color still gets used (with 11 parts):

Green Colored Pie

And the angles / each brush index used here

angled for green pie

What am I doing wrong here?

Answer Source

You keep growing the size of the AngleEnd value. Don't do that:

//float AngleEnd = AngleStart + partSize;
float AngleEnd = partSize;

From Graphics.FillPie Method

sweepAngle
Angle in degrees measured clockwise from the startAngle parameter to the second side of the pie section.

Also, please use

e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

to get rid of those rough edges.