Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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.
 
    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 2-Dec-12 9:11am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 367
1 Abhinav S 226
2 thatraja 220
3 OriginalGriff 195
4 Peter Leow 160
0 Sergey Alexandrovich Kryukov 8,849
1 OriginalGriff 5,035
2 Peter Leow 3,949
3 Maciej Los 3,535
4 Abhinav S 3,148


Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 2 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid