title: Refreshing picturebox image in visual basic
body: <p>I am coding a program that edits pictures, I have a picture box where I load a bitmap, the first image loads fine, but if I want to load a second image, the second image won't load and the first one stays, it won't refresh. How can I do to change the image in the picture box using 2 different bitmaps?</p>
  
  <p>I have PictureBox3.Image = bm where bm is a bitmap variable, it loads fine, but then if I press the other button to load another bitmap in PictureBox3 (PictureBox3.Image = bm2), it won't load.</p>
  
  <pre><code>Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
      Dim clr As Integer
      Dim ymax As Integer
      Dim xmax As Integer
      Dim x As Integer
      Dim y As Integer
      Dim bm As Bitmap = PictureBox1.Image
      xmax = bm.Width - 1
      ymax = bm.Height - 1
      For y = 0 To ymax
          For x = 0 To xmax
              With bm.GetPixel(x, y)
                  clr = 0.21 * .R + 0.72 * .G + 0.07 * .B
              End With
              bm.SetPixel(x, y, _
              Color.FromArgb(255, clr, clr, clr))
          Next x
      Next y
      PictureBox3.Image = bm
  End Sub
  
  Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
      Dim clr As Integer
      Dim ymax As Integer
      Dim xmax As Integer
      Dim x As Integer
      Dim y As Integer
      Dim bm2 As Bitmap = PictureBox1.Image
      xmax = bm2.Width - 1
      ymax = bm2.Height - 1
      For y = 0 To ymax
          For x = 0 To xmax
              With bm2.GetPixel(x, y)
                  clr = (1 * .R + 1 * .G + 1 * .B) / 3
              End With
              bm2.SetPixel(x, y, _
              Color.FromArgb(255, clr, clr, clr))
          Next x
      Next y
      PictureBox3.Image = bm2
  End Sub
  </code></pre>
  
  <p>End Class</p>