[英]How can I draw the current Graphics object without clear?
I want to draw a function graphic using a list of points.我想使用点列表绘制函数图形。 But it is slowing down when so many points exist.
但是当存在如此多的点时,它正在放缓。 So I think if I can draw without erasing current drawing I don't need to store the points.
所以我想如果我可以在不擦除当前绘图的情况下绘图,我就不需要存储点。 I know
Invalidate
clears the current drawing.我知道
Invalidate
清除当前绘图。 How can I do this trick?我怎样才能做到这一点?
I an using this method currently:我目前使用这种方法:
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;
}
UPDATE更新
As you suggest I have used a Bitmap to redraw.正如你所建议的,我使用了位图来重绘。 But the quality of the result is not good for me.
但结果的质量对我来说并不好。 Additionally, I have no idea how to call
Invalidate
only when the data is changed.此外,我不知道如何仅在数据更改时调用
Invalidate
。 Because here, the data changes when Invalidate
method is called.因为在这里,调用
Invalidate
方法时数据会发生变化。
Code:代码:
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;
}
As you see in the result, the number texts haven't a good quality:正如您在结果中看到的,数字文本质量不佳:
UPDATE 2: Ok, I changed my code a little.更新 2:好的,我稍微改变了我的代码。 Just drawing once the coordinate system at
Load
event it has a good quality now.只需在
Load
事件中绘制一次坐标系,它现在就具有良好的质量。 Thanks everyone for help!感谢大家的帮助!
There is no way to "not clear" the graphics.没有办法“不清除”图形。 When a
Control
needs to be drawn (has been invalidated) you have to draw everything again.当需要绘制
Control
(已失效)时,您必须再次绘制所有内容。
One way to improve your code a little is to use Graphics.DrawLines()
instead of iterating through the points and call Graphics.DrawLine()
for each seperate line.稍微改进代码的一种方法是使用
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());
}
The Paint event has a e.ClipRectangle
property. Paint 事件有一个
e.ClipRectangle
属性。
This is the actual rectangle that needs to be repainted, as it is not always necessary to redraw everything (eg part of a form is moved over your form and moved off again).这是需要重新绘制的实际矩形,因为并不总是需要重新绘制所有内容(例如,表单的一部分移到您的表单上并再次移开)。
So one thing you can do is only draw the lines that fall within that rectangle.因此,您可以做的一件事就是只绘制位于该矩形内的线条。
But having said that if you are calling Invalidate, that signals that the whole things needs to be redrawn.但话虽如此,如果您调用 Invalidate,则表示需要重新绘制整个内容。
It would be better to keep a track of which ones you have drawn perhaps and only draw the new ones?最好记录一下你画了哪些,只画新的?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.