Search code examples
vb.netcustom-controlsonpaint

Why won't Paint draw this image?


I am overriding the OnPaint method in my control that I've made to draw it directly from an image generated from my own ICan3D.Graphics class. When I save the image (as you can see, that line is commented out) the image is correct. When the form loads, however, it doesn't display the image as it's background.

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

I'm using VB.net so all .net answers are acceptable :)


Solution

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