[英]How can I use a graphic object's clear function(which requires a color argument) to clear the graphics of a form?
[英]How can I draw the current Graphics object without clear?
我想使用點列表繪制函數圖形。 但是當存在如此多的點時,它正在放緩。 所以我想如果我可以在不擦除當前繪圖的情況下繪圖,我就不需要存儲點。 我知道Invalidate
清除當前繪圖。 我怎樣才能做到這一點?
我目前使用這種方法:
CoordinateSystem cos = new CoordinateSystem();
List<PointF> points = new List<PointF>();
float a = -20;
void Form1_Tick(object sender, EventArgs e)
{
panel1.Invalidate();
}
void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
double x = a;
double y = Root(x, 3);
cos.DrawSystem(e.Graphics);
points.Add(cos.RelativePoint(new PointF((float)x, (float)y)));
for (int i = 1; i < points.Count - 1; i++)
{
e.Graphics.DrawLine(Pens.Red,points[i],points[i+1]);
}
a += 0.05f;
}
更新
正如你所建議的,我使用了位圖來重繪。 但結果的質量對我來說並不好。 此外,我不知道如何僅在數據更改時調用Invalidate
。 因為在這里,調用Invalidate
方法時數據會發生變化。
代碼:
CoordinateSystem cos = new CoordinateSystem();
Bitmap bmp;
float a = -20;
void Form1_Tick(object sender, EventArgs e)
{
panel1.Invalidate();
}
void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
double x = a;
double y = Root(x, 3);
PointF rel = cos.RelativePoint(new PointF((float)x, (float)y));
using (Graphics grp = Graphics.FromImage(bmp))
{
grp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
cos.DrawSystem(grp);
grp.DrawEllipse(Pens.Red, rel.X - 0.5f, rel.Y - 0.5f, 1, 1);
}
e.Graphics.DrawImage(bmp, 0, 0);
a += 0.01f;
}
更新 2:好的,我稍微改變了我的代碼。 只需在Load
事件中繪制一次坐標系,它現在就具有良好的質量。 感謝大家的幫助!
沒有辦法“不清除”圖形。 當需要繪制Control
(已失效)時,您必須再次繪制所有內容。
稍微改進代碼的一種方法是使用Graphics.DrawLines()
而不是遍歷點並為每個單獨的線調用Graphics.DrawLine()
。
void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
double x = a;
double y = Root(x, 3);
cos.DrawSystem(e.Graphics);
points.Add(cos.RelativePoint(new PointF((float)x, (float)y)));
e.Graphics.DrawLines(Pens.Red, points.ToArray());
}
Paint 事件有一個e.ClipRectangle
屬性。
這是需要重新繪制的實際矩形,因為並不總是需要重新繪制所有內容(例如,表單的一部分移到您的表單上並再次移開)。
因此,您可以做的一件事就是只繪制位於該矩形內的線條。
但話雖如此,如果您調用 Invalidate,則表示需要重新繪制整個內容。
最好記錄一下你畫了哪些,只畫新的?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.