How would I use an Open File Dialog to select an Image and then put that Image in a Picturebox on another Form?

picturebox, vb.net

Solution

If I understood you correctly then it is pretty easy:

Sub OpenAnImageInPicturebox(ByRef pb As PictureBox)
    Dim ofd As New OpenFileDialog
    ofd.Filter = "Bitmap|*.bmp|JPEG|*.jpg" 'If you like file type filters you can add them here
    'any other modifications to the dialog
    If ofd.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub
    Try
        Dim bmp As New Bitmap(ofd.FileName)
        If Not IsNothing(pb.Image) Then pb.Image.Dispose() 'Optional if you want to destroy the previously loaded image
        pb.Image = bmp
    Catch
        MsgBox("Not a valid image file.")
    End Try
End Sub

Problem

How would I use an Open File Dialog to select an Image and then put that Image in a Picturebox Control on another Form? ``` Private Sub btnLogo_Click(sender As Object, e As EventArgs) Handles btnLogo.Click OpenFileDialog1.Title = "Please Select a File" OpenFileDialog1.InitialDirectory = "C:" OpenFileDialog1.ShowDialog() photo = OpenFileDialog1.FileName.ToString ``` I'm guessing this is wrong but I'm lost to what to do here. Then once I have selected an Image; what would be the appropriate Code to put that Image into a Picturebox Control on the other Form ?

Original source