Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 297
jruder Avatar asked Sep 04 '25 16:09

jruder


2 Answers

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
like image 94
Jens Avatar answered Sep 07 '25 17:09

Jens


try this:

photo = image.Fromfile( OpenFileDialog1.FileName)

Hope it helps

like image 44
bto.rdz Avatar answered Sep 07 '25 18:09

bto.rdz