gpt4 book ai didi

c# - 使用 Windows 窗体图形绘制笔划 - 需要改进性能

转载 作者:太空宇宙 更新时间:2023-11-03 10:48:39 24 4
gpt4 key购买 nike

我有一个 Panel,我需要用鼠标在其中绘制一些 Stroke。我的 Strokes 只是 Point 的列表,我使用鼠标事件添加它们。以下是我的代码:

private void Panel_MouseDown(object sender, MouseEventArgs e)
{
isDrawing = true;
currStroke = new Stroke(new Pen(Color.Black, 2));
currStroke.Points.Add(e.Location);
visuals.Add(currStroke);
}

private void Panel_MouseMove(object sender, MouseEventArgs e)
{
if (isDrawing)
{
currStroke.Points.Add(e.Location);
Invalidate(false);
}
}

private void Panel_MouseUp(object sender, MouseEventArgs e)
{
isDrawing = false;
}

我重写了 OnPaint 方法,我在其中绘制了我所有的 visuals,特别是为了绘制笔触,我使用了以下代码:

e.Graphics.DrawLines(stroke.Pen, stroke.Points.ToArray());

它有效,但是当我绘制我的面板时闪烁,也就是说我可以看到我的面板快速出现和消失。可能是因为不断调用了Invalidate()方法。

是否有任何解决方法可以提高性能并消除闪烁效果

最佳答案

您应该设置 DoubleBuffered控件的属性设置为 true

来自链接:

Buffered graphics can reduce or eliminate flicker that is caused by progressive redrawing of parts of a displayed surface. Buffered graphics require that the updated graphics data is first written to a buffer. The data in the graphics buffer is then quickly written to displayed surface memory. The relatively quick switch of the displayed graphics memory typically reduces the flicker that can otherwise occur.

关于c# - 使用 Windows 窗体图形绘制笔划 - 需要改进性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22383690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com