简体   繁体   English

如何在不清晰的情况下绘制当前 Graphics 对象?

[英]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.

相关问题 如何使用图形对象的clear函数(需要color参数)来清除表单的图形? - How can I use a graphic object's clear function(which requires a color argument) to clear the graphics of a form? 如何在没有表单的情况下在C#中绘制图形 - How do I draw graphics in C# without a form 如何在图形对象转换后在图形对象中的任何位置绘制 - How do i draw anywhere in a graphics object after it is transformed 在绘制另一个字母之前,如何清除图形? - How can I clear the Graphics before drawing another letter? 将旋转的图像直接绘制到Graphics对象,而无需创建新的位图 - Draw a rotated image directly to Graphics object, without creating a new Bitmap 如何在C#中将具有透明截面的图像绘制到Graphics对象? - How do I draw an image with a transparent section to a Graphics object in C#? 如何以十六进制颜色在图形对象上绘制实心圆? - How do I draw a filled circle onto a graphics object in a hexadecimal colour? 以ac#形式使用painteventargs; 如何将更大的图像绘制到Graphics对象(以及滚动条)? - using painteventargs in a c# form; how do I draw a larger image to the Graphics object (with scrollbars as well)? 如何将 Graphics 对象转换为 Bitmap(或将其作为 Bitmap 访问) - How can I get a convert a Graphics object to a Bitmap (or access it as a Bitmap) 如何创建写入图形对象的.PS,.SVG和.PNG? - How can I create a .PS, .SVG, & .PNG writing to a Graphics object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM