2012-05-31 53 views
-1

我重写了我的控件中的OnPaint方法,该方法是直接从我自己的ICan3D.Graphics类生成的图像中绘制的。当我保存图像时(正如你所看到的那样,该行被注释掉了)图像是正确的。但是,当表单加载时,它不会将图像显示为背景。为什么不绘制此图像?

Imports System 

Namespace ICan3D 

    Public Class RenderSurface 
     Inherits Control 

     Dim nImg As Graphics 

     Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs) 
      Dim img As Bitmap = nImg.Visual 
      'img.Save("C:\image.png") 
      Dim nGraphDis As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(img) 
      Dim nPaintEventArgs As New PaintEventArgs(nGraphDis, New Rectangle(0, 0, Width, Height)) 
      MyBase.OnPaint(nPaintEventArgs) 
     End Sub 

     Private Sub RenderSurface_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize 
      nImg = New Graphics(Width, Height) 
     End Sub 

    End Class 

End Namespace 

我使用VB.net使所有的.NET的答案是可以接受的:)

+0

您需要使用PaintEventArgs的参数的图形对象,而不是提供自己。见托马斯的回答。 – MicSim

回答

3
Protected Overrides Sub OnPaint(e As PaintEventArgs) 
    MyBase.OnPaint(e) 
    e.Graphics.DrawImageUnscaled(nImg.Visual, 0, 0, Width, Height) 
End Sub 
+0

谢谢,它的作品:) – FreeSnow