Vb.net 为什么赢了';你不画这幅画吗?

Vb.net 为什么赢了';你不画这幅画吗?,vb.net,custom-controls,onpaint,Vb.net,Custom Controls,Onpaint,我正在重写控件中的OnPaint方法,该方法直接从我自己的ICan3D.Graphics类生成的图像中绘制。当我保存图像时(如您所见,该行被注释掉),图像是正确的。但是,当表单加载时,它不会将图像显示为背景 Imports System Namespace ICan3D Public Class RenderSurface Inherits Control Dim nImg As Graphics Protected Overrid

我正在重写控件中的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答案都可以接受:)

您需要使用PaintEventArgs参数的图形对象,而不是提供自己的。见托马斯的回答。
Protected Overrides Sub OnPaint(e As PaintEventArgs)
    MyBase.OnPaint(e)
    e.Graphics.DrawImageUnscaled(nImg.Visual, 0, 0, Width, Height)
End Sub