繁体   English   中英

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM