如何仅清除绘图字符串而不重绘背景图像?

时间:2019-10-25 20:54:42

标签: vb.net graphics

我正在绘制所有内容(图像和所有字符串),但是当我绘制图像时,会加载处理器并闪烁图像,这是我不想要的两件事...

要解决此问题,我尝试了以下方法:

  1. 将主图像设置为背景图像,然后在其上绘制,但是当您尝试“清除(color.transparent)”时,它将变为黑色,您必须再次绘制它。
  2. 在主图片框上添加另一个图片框,但是当您这样做时,正面图片会隐藏所有内容(如图2所示)

注意:背景图像是静态的,永远不会改变,唯一会改变的是拉绳的...

Static background image

Drawstring's

Overlap problem

1 个答案:

答案 0 :(得分:0)

请考虑以下代码段,这些代码段可以帮助您解决问题。

Private ReadOnly backImage As Bitmap = 'The path of your background image.

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    Dim srcRect As New Rectangle(0, 0, backImage.Width, backImage.Height)
    Dim desRect As Rectangle = PictureBox1.ClientRectangle

    Using bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height)
        Using G As Graphics = Graphics.FromImage(bmp)
            G.SmoothingMode = SmoothingMode.HighQuality
            G.Clear(Color.Transparent)
            G.DrawImage(backImage, desRect, srcRect, GraphicsUnit.Pixel)
            G.DrawString("0.00", New Font("Arial", 12, FontStyle.Regular), Brushes.Red, desRect.Right - 80, 35)
            'and the other overlay painting ....
        End Using

        e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic
        e.Graphics.DrawImageUnscaled(bmp, 0, 0)
    End Using
End Sub

另外,为了减少闪烁,请从PictureBox控件派生一个新类,并将样式设置为:

Public Class DBPictureBox
    Inherits PictureBox

    Sub New()
        SetStyle(ControlStyles.AllPaintingInWmPaint Or
                 ControlStyles.OptimizedDoubleBuffer Or
                 ControlStyles.ResizeRedraw Or
                 ControlStyles.UserPaint, True)
        UpdateStyles()
    End Sub

    'Comment or remove the MyBase....
    Protected Overrides Sub OnPaintBackground(pevent As PaintEventArgs)
        'MyBase.OnPaintBackground(pevent)
    End Sub

End Class

并使用它代替默认的PictureBox。


修改

我不知道您如何更新绘画例程,也不知道其余代码是否会对绘画产生影响。我认为上述代码片段绰绰有余。这是一个快速演示,它每100毫秒使无效(不刷新):

enter image description here

您看到闪烁了吗?这是演示运行时我任务管理器提供的信息:

enter image description here

几乎没有。

我建议:

  • 如果通过输入控件的 TextChanged 和/或 ValueChanged 事件使绘画无效,请确保您的控件没有处于无限冗余循环中。每个事件一次又一次地调用另一个事件。如果没有,那么:

  • 在另一台计算机上尝试应用程序,以确保您的主要计算机没有硬件问题。也许是时候进行新设置了:)。

祝你好运。