c# - PictureBox Refresh导致上面的图层闪烁

标签 c# winforms picturebox

我正在尝试从端口读取数据并将其显示在表盘上。背景是一个 PictureBox,其中包含一个给出读数的圆形刻度图像,一个 Lineshape 已用于表示刻度盘。 PictureBox 已被“发送到后面”以允许线形的可见性。我附上代表代码: 如果我这样做:

    private void timer1_Tick(object sender, EventArgs e)
    {
        ang += 2.0 * Math.PI / 180.0;
        lineShape1.X1 = Convert.ToInt32(lineShape1.X2 - r * Math.Sin(ang));
        lineShape1.Y1 = Convert.ToInt32(lineShape1.Y2 - r * Math.Cos(ang));

    }

然后它在 PictureBox 上留下痕迹。

但是如果我使用 PictureBox 的刷新功能,那么表盘会出现闪烁。我已尝试使用 10、15、20、50 和 100 的计时器间隔,但问题仍然存在。

    private void timer1_Tick(object sender, EventArgs e)
    {
        ang += 2.0 * Math.PI / 180.0;
        lineShape1.X1 = Convert.ToInt32(lineShape1.X2 - r * Math.Sin(ang));
        lineShape1.Y1 = Convert.ToInt32(lineShape1.Y2 - r * Math.Cos(ang));
        pictureBox1.Refresh();
    }

为了实际问题,不能增加间隔。我该怎么做才能显示表盘的平滑变化?

最佳答案

正如@Hans Passant 所建议的,您不应该使用 LineShape 对象。这是实现他的建议的代码示例(快速,未经测试):

private void timer1_Tick(object sender, EventArgs e)
{
    pictureBox1.Invalidate();
}

void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    ang += 2.0 * Math.PI / 180.0;
    var X1 = Convert.ToInt32(X2 - r * Math.Sin(ang));
    var Y1 = Convert.ToInt32(Y2 - r * Math.Cos(ang));

    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

    e.Graphics.DrawLine(
        new Pen(Color.Yellow, 1f),
        new Point(X1, Y1), 
        new Point(X2, Y2 ));
}

此外,尝试设置 this.DoubleBuffered = true; 以避免闪烁问题。

关于c# - PictureBox Refresh导致上面的图层闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24984181/

相关文章:

c# - 使用paint时如何在WinForms中实现垂直和水平滚动条?

c# - 避免 PropertyGrid 的异常

visual-c++ - 如何清除vc++paint中所有以前绘制的图形?

C# dt.Compute() 在某些乘法上不能正常工作

c# - 如何在加载表单时将焦点放在 TextBox 上?

c# - 为什么在 C# 中将方法分配给变量?

c# - Windows 窗体 anchor ,以相同的比例保持面板内的控件?

c# - 错误 : Object is currently in use elsewhere.

c# - 如何轻松恢复 DataBound 表单上的更改?

c# - 掌上电脑 : Draw control to bitmap