Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I am trying to draw a dynamically created image to a rectangle but it seems to keep drawing it to the form. Any advice would be great.

VB
Private Sub BGMap_Paint(sender As Object, e As PaintEventArgs) Handles BGMap.Paint
    'System Rectangle drawn in design time.
    Dim g As Graphics
    For X = 0 To 9
        For Y = 0 To 7
            If genArr(X, Y) = 2 Then
                lRect = New Rectangle(0, 0, 64, 64)
                dLRect = New Rectangle(X * 64, Y * 64, 64, 64)
                g.DrawImage(lBMP, dLRect, lRect, GraphicsUnit.Pixel)
            ElseIf genArr(X, Y) = 1 Or genArr(X, Y) = 0 Then
                lRect = New Rectangle(64, 0, 64, 64)
                dLRect = New Rectangle(X * 64, Y * 64, 64, 64)
                g.DrawImage(lBMP, dLRect, lRect, GraphicsUnit.Pixel)
            End If
        Next
    Next
End Sub


I invalidate the rectangle onLoad which is where the array that the image is drawn from is populated.
Posted

1 solution

First, this code should be throwing a "Object not set to an instance of an object" exception because you never set the value of g. Your code should begin like this:
Private Sub BGMap_Paint(sender As Object, e As PaintEventArgs) Handles BGMap.Paint
    'System Rectangle drawn in design time.
    Dim g As Graphics = e.Graphics



Next, what do you mean by "It keeps drawing it to the form"?? Your code SHOULD be prepared to redraw the contents of the form at any time as Windows will repaint your form on an as-needed basis, determined by Windows.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900