This may seem like a simple question but I am new at VB so be gentle.

I am trying to do a simple open image file, display in picturebox, save as but when I do this using the code below, it produces errors

Object reference not set to an instance of an object. on my picturebox1.image.dispose()

What is the best way to read an image from a file, display it in a picture box and then save it with a different name?


Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Set the Filter.
        OpenFileDialog1.Filter = "Bitmap |*.bmp| JPG | *.jpg | GIF | *.gif | All Files|*.*"

        'Clear the file name
        OpenFileDialog1.FileName = ""

        'Show it
        If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
            'Get the image name
            Dim img As String = OpenFileDialog1.FileName

            'Create a new Bitmap and display it
            Dim Image As Image
            PictureBox1.Image = System.Drawing.Bitmap.FromFile(img)
            Image = PictureBox1.Image
            'PictureBox1.Image = Nothing

        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles Button2.Click
        ' Displays a SaveFileDialog so the user can save the Image
        ' assigned to Button2.
        Dim saveFileDialog1 As New SaveFileDialog()
        saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"
        saveFileDialog1.Title = "Save an Image File"

        ' If the file name is not an empty string open it for saving.
        If saveFileDialog1.FileName <> "" Then
            PictureBox1.Image = Nothing
            ' Saves the Image via a FileStream created by the OpenFile method.
            Dim fs As System.IO.FileStream = CType _
               (saveFileDialog1.OpenFile(), System.IO.FileStream)
            ' Saves the Image in the appropriate ImageFormat based upon the
            ' file type selected in the dialog box.
            ' NOTE that the FilterIndex property is one-based.
            Select Case saveFileDialog1.FilterIndex
                Case 1
                    Me.Button2.Image.Save(fs, _

                Case 2
                    Me.Button2.Image.Save(fs, _

                Case 3
                    Me.Button2.Image.Save(fs, _
            End Select

        End If
    End Sub
End Class