在Winform的绘画事件中,为什么FillPolygon变慢?是因为它计算了应该在三角形内绘制的所有像素?
这是整个应用程序中花费时间最多的部分。因此,我想对其进行优化。
SolidBrush myBrush = new SolidBrush(Color.Black);
MatrixCellZoom zoom = rectangle.CellZoom;
List<Point> points = new List<Point>();
points.Add(zoom.pont1.ToPoint());
points.Add(zoom.pont2.ToPoint());
points.Add(zoom.pont3.ToPoint());
graphics.FillPolygon(myBrush, points.ToArray()); /* the show part */
答案 0 :(得分:1)
修订后的答案-与.FillRectangle一样快。至少对于三角形。
int incr = 1000000;
using (var bmp = new Bitmap(200, 500))
using (var gr = Graphics.FromImage(bmp))
{
DateTime timeBeforeFillRectangle = DateTime.Now;
for (int i = 0; i < incr; i++)
{
gr.FillRectangle(Brushes.Orange, new Rectangle(0, 0, 100, 300));
}
Point[] points = new Point[] { new Point(10, 100), new Point(190, 200), new Point(80, 400) };
DateTime timeBeforeFillPolygon = DateTime.Now;
for (int i = 0; i < incr; i++)
{
gr.FillPolygon(Brushes.Orange, points);
}
TimeSpan ts1 = timeBeforeFillPolygon.Subtract(timeBeforeFillRectangle);
TimeSpan ts2 = DateTime.Now.Subtract(timeBeforeFillPolygon);
Console.WriteLine("FillRectangle seconds: " + ts1.TotalSeconds.ToString());
Console.WriteLine("FillPolygon seconds: " + ts2.TotalSeconds.ToString());
}
}