具有 n 个部分的 FillPie 画笔颜色问题
FillPie with n sections Brush Color issue
当尝试在我的图形对象上使用 FillPie 时,如果我实际上绘制了整个饼图,它全部显示为纯色(我使用的是打印预览对象)
我试过四处移动对象来查明问题,这是我现在所拥有的:
//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);
}
}
我得到一个纯色派,从右上角到中心有一条线 运行(来自 DrawPie):
我可以通过注释掉 FillPie 行并仅使用 DrawPie 来验证角度是否正确:
如果您查看数组并推断正在发生的事情,整个饼图是由最后使用的颜色画笔金色绘制的。我将角度和正在使用的画笔输出到控制台,它在画笔阵列中正确循环:
如果我将饼图部分的数量更改为不同的数量,最终颜色仍会使用(有 11 个部分):
以及此处使用的角度/每个笔刷索引
我做错了什么?
您不断增大 AngleEnd 值的大小。不要那样做:
//float AngleEnd = AngleStart + partSize;
float AngleEnd = partSize;
sweepAngle
Angle in degrees measured clockwise from the startAngle parameter to the second side of the pie section.
此外,请使用
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
摆脱那些粗糙的边缘。
当尝试在我的图形对象上使用 FillPie 时,如果我实际上绘制了整个饼图,它全部显示为纯色(我使用的是打印预览对象)
我试过四处移动对象来查明问题,这是我现在所拥有的:
//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);
}
}
我得到一个纯色派,从右上角到中心有一条线 运行(来自 DrawPie):
我可以通过注释掉 FillPie 行并仅使用 DrawPie 来验证角度是否正确:
如果您查看数组并推断正在发生的事情,整个饼图是由最后使用的颜色画笔金色绘制的。我将角度和正在使用的画笔输出到控制台,它在画笔阵列中正确循环:
如果我将饼图部分的数量更改为不同的数量,最终颜色仍会使用(有 11 个部分):
以及此处使用的角度/每个笔刷索引
我做错了什么?
您不断增大 AngleEnd 值的大小。不要那样做:
//float AngleEnd = AngleStart + partSize;
float AngleEnd = partSize;
sweepAngle
Angle in degrees measured clockwise from the startAngle parameter to the second side of the pie section.
此外,请使用
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
摆脱那些粗糙的边缘。