我使用vb.net 2008 express进行频谱分析应用程序,将频谱图像绘制到图片框中。我使用缓冲图形来提高速度并从绘图事件中调用绘图例程。在大多数情况下,这工作正常,但当另一个窗口移动到我的上方时,当我返回窗口时,被覆盖的部分是空白的。我已经确定在从picturebox.paint事件调用绘制例程之后发生了擦除。如果我再次调用绘图程序(在这种情况下由Spectra_MouseUp引起),则图像被完全重绘。
在我回到窗口后,我无法弄清楚如何保留(或重绘)整个图像。
我的代码的相关部分如下:
Private currentContext As BufferedGraphicsContext
Private spectraBuffer As BufferedGraphics
Private Sub DrawSpectra()
Dim Gr As Graphics
spectraBuffer = currentContext.Allocate(Spectra.CreateGraphics, Spectra.DisplayRectangle)
Gr = spectraBuffer.Graphics
'.
'.
'. Lots of drawing stuff here such as.... (yes, everything here is declared)
If FillSpectra Then
Gr.DrawPolygon(BasePen, ptsSpec.ToArray)
Gr.FillPolygon(BaseBrush, ptsSpec.ToArray)
Else
Gr.DrawLines(BasePen, ptsSpec.ToArray)
End If
'.
'.
' Render the contents of the buffer to the Spectra window.
spectraBuffer.Render()
End Sub
Private Sub frmMCAWindow_Activated(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Activated
Call DrawSpectra()
' DrawSpectra() works fine from here
End Sub
Private Sub Spectra_MouseUp(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.MouseEventArgs) Handles Spectra.MouseUp
Call DrawSpectra()
End Sub
Private Sub Spectra_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Spectra.Paint
' When dragging another window over mine, DrawSpectra() works fine here to keep the image in the picturebox
Debug.Print("Begin Spectra_Paint()")
Call DrawSpectra()
' I put a 1 sec. sleep for testing, the image stayed complete while waiting
' The part that was covered was erased once the sleep completed.
Debug.Print("End Spectra_Paint()")
End Sub
答案 0 :(得分:0)
谢谢克里斯。结果是使用Spectra.CreateGraphics而不是PaintEventArgs(对于e.Graphics)是问题。我重写了以下&它现在完美无缺。
Private currentContext As BufferedGraphicsContext
Private spectraBuffer As BufferedGraphics
Private Sub frmMCAWindow_Activated(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Activated
Spectra.Invalidate()
End Sub
Private Sub Spectra_MouseUp(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.MouseEventArgs) Handles Spectra.MouseUp
Spectra.Invalidate()
End Sub
Private Sub Spectra_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Spectra.Paint
Dim Gr As Graphics
spectraBuffer = currentContext.Allocate(e.Graphics, Spectra.DisplayRectangle)
Gr = spectraBuffer.Graphics
'.
'.
'. Lots of drawing stuff here such as....
If FillSpectra Then
Gr.DrawPolygon(BasePen, ptsSpec.ToArray)
Gr.FillPolygon(BaseBrush, ptsSpec.ToArray)
Else
Gr.DrawLines(BasePen, ptsSpec.ToArray)
End If
'.
'.
' Render the contents of the buffer to the Spectra window.
spectraBuffer.Render()
End Sub