我想使用点列表绘制函数图形。但是当存在这么多点时,它正在放缓。所以我想如果我可以在不删除当前绘图的情况下进行绘制,我就不需要存储这些点。我知道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;
}
更新
正如您所建议我使用Bitmap重绘。但结果的质量对我来说并不好。此外,我不知道如何在数据更改时调用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
事件中绘制一次坐标系,它现在就具有良好的质量。谢谢大家的帮助!
答案 0 :(得分:2)
没有办法&#34;不清楚&#34; 图形。当需要绘制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());
}
答案 1 :(得分:1)
Paint事件具有e.ClipRectangle
属性。
这是需要重新绘制的实际矩形,因为并不总是需要重绘所有内容(例如,表单的一部分移动到表单上并再次移出)。
所以你可以做的一件事就是画出那个落在那个矩形内的线条。
但是,如果您正在调用Invalidate,则表示需要重新绘制所有内容。
最好跟踪你所画的是哪些,只画出新的?