title: VB.NET paint method?
body: <p>Is this the best way to paint images on a form? I have most of my experience in Java and little in VB.net. In Java there is repaint() that can be called? There is a Me.refresh(). Is this what you wnat to call when you have something to add to the form or to paint to the form.</p>
  
  <p>Class:</p>
  
  <pre><code>    Public Class Form1
  
      Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
          ' Create image. 
          Dim newImage As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
  
          ' Create Point for upper-left corner of image. 
          Dim ulCorner As New Point(50, 50)
  
          ' Draw image to screen.
          e.Graphics.DrawImage(newImage, ulCorner)
  
          Dim newImage2 As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
  
          ' Create Point for upper-left corner of image. 
          Dim ulCorner2 As New Point(150, 150)
  
          ' Draw image to screen.
          e.Graphics.DrawImage(newImage2, ulCorner2)
  
      End Sub
  
  End Class
  </code></pre>
  
  <p>I tried this code here with one button on the form with a click event</p>
  
  <pre><code>Public Class Form1
      Dim i As Integer
  
      Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
  
          i = 0
  
          ' Create image. 
          Dim newImage As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
  
          ' Create Point for upper-left corner of image. 
          Dim ulCorner As New Point(50, 50)
  
          ' Draw image to screen.
          e.Graphics.DrawImage(newImage, ulCorner)
  
          Dim newImage2 As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
  
          ' Create Point for upper-left corner of image. 
          Dim ulCorner2 As New Point(150, 150)
  
          ' Draw image to screen.
          e.Graphics.DrawImage(newImage2, ulCorner2)
  
          If i = 5 Then
              ' Create image. 
              Dim newImage3 As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
  
              ' Create Point for upper-left corner of image. 
              Dim ulCorner3 As New Point(250, 250)
  
              ' Draw image to screen.
              e.Graphics.DrawImage(newImage3, ulCorner3)
          End If
  
      End Sub
  
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
          i = 5
          Me.Refresh()
      End Sub
  End Class
  </code></pre>
  
  <p>Just Tried this code here:</p>
  
  <pre><code> Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
          i = 5
          Me.Invalidate()
   End Sub
  </code></pre>
  
  <p>New Class just Tested:</p>
  
  <pre><code>Public Class Form2
      Dim newImage As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
      Dim newImage2 As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
      Dim newImage3 As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
  
      Private bgImage As Bitmap
      Private srcImages As Image()
  
      Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
  
          Dim img As Bitmap = Me.bgImage
  
          If (img Is Nothing) Then
              Me.bgImage = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
              img = Me.bgImage
          End If
  
          If (Not Me.srcImages Is Nothing) Then
          End If
  
          MyBase.OnPaint(e)
  
      End Sub
  
      Private Sub _Load(sender As Object, e As EventArgs) Handles MyBase.Load
          Me.srcImages = New Image(3 - 1) {}
          Me.srcImages(0) = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
          Me.srcImages(1) = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
          Me.srcImages(2) = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
      End Sub
  
      Private Sub _Disposed(sender As Object, e As EventArgs) Handles MyBase.Disposed
          If (Not Me.srcImages Is Nothing) Then
              For Each img As Image In Me.srcImages
                  If (Not img Is Nothing) Then
                      img.Dispose()
                  End If
              Next
              Me.srcImages = Nothing
          End If
          If (Not Me.bgImage Is Nothing) Then
              Me.bgImage.Dispose()
              Me.bgImage = Nothing
          End If
      End Sub
  
      Private Sub _Resize(sender As Object, e As EventArgs) Handles MyBase.SizeChanged, MyBase.Resize
          If (Not Me.bgImage Is Nothing) Then
              Me.bgImage.Dispose()
              Me.bgImage = Nothing
          End If
      End Sub
  
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
          Me.Invalidate()
      End Sub
  End Class
  </code></pre>