[英]c# draw many circle at the same time
我試圖建立自己的極坐標圖 。 我使用一個函數來繪制點,如下所示:
private void drawPoints()
{
this.SuspendLayout();
Graphics g = this.CreateGraphics();
int i = 0;
foreach (Point Pointaktuell in PointList)
{
int radius = 15;
Brush b = new SolidBrush(Color.Red);
g.FillEllipse(b, (int)(Pointaktuell.X - radius / 2.0), (int)(Pointaktuell.Y - radius / 2.0), radius, radius);
i++;
}
PointList.Clear();
this.ResumeLayout();
}
問題在於要點一一畫完,需要很多時間。 我如何一次繪制它們?
為了優化繪制過程中,盡量把更多的代碼,你可以在循環外 。 別忘了關閉IDisposable
private void drawPoints()
{
SuspendLayout();
try
{
using (Graphics g = CreateGraphics())
{
int i = 0;
int radius = 15;
using (b = new SolidBrush(Color.Red))
{
foreach (Point Pointaktuell in PointList)
{
g.FillEllipse(b, (int)(Pointaktuell.X - radius / 2.0), (int)(Pointaktuell.Y - radius / 2.0), radius, radius);
i += 1;
}
}
}
}
finally { ResumeLayout(); }
}
在每個循環步驟中實例化一個新的SolidBrush
。 radius
一次又一次地重新定義。 實例化對象不調用Dispose()
例程(這對於Graphics
很重要)
您可以嘗試以下方法;
private void drawPoints()
{
this.SuspendLayout();
const int radius = 15;
using (Graphics g = this.CreateGraphics())
{
//if (g == null) { this.ResumeLayout(); return; } // # Uncomment this line if you want defensive checks
using (Brush b = new SolidBrush(Color.Red))
{
for (int i = 0; i < PointList.Count; i++)
{
g.FillEllipse(b, (int)(PointList[i].X - radius / 2.0), (int)(PointList[i].Y - radius / 2.0), radius, radius);
}
}
}
PointList.Clear();
this.ResumeLayout();
}
僅當您要在drawPoints()
完成所有繪圖工作並一次調用一次時。 如果您要定期調用drawPoints()
或在流程的其他部分使用更多繪圖例程; 我建議您在不再需要Graphics g
和Brush b
引用以及Dispose()
時保留它們(在這種情況下不再繪制)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.