jruder
jruder

Reputation: 77

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 ?

Upvotes: 4

Views: 23192

Answers (4)

evry1falls
evry1falls

Reputation: 194

I came across this question when I was looking for Filter, as an update to previous answers.

You want to use OpenFileDialog to select an Image to put in a PictureBox Control in another Form. I suggest :-

  1. Use Module : to use the Image on any Form
  2. Use Default Image : to display in PictureBox Control whenever Error occurred. Use Project Resources to add Existing Resource (I.E. PNG Image file : noPhotoUsr).

Code for Module1

Module Module1
    Public Function _GetImgOFD(Frm As Form, PicBx As PictureBox) As Bitmap
        Dim _ErrBitmap As Bitmap = My.Resources.noPhotoUsr
        Dim ChosenBitmap As Bitmap
        Using OFD As OpenFileDialog = New OpenFileDialog
            With OFD
                .Filter = ("Image File (*.ico;*.jpg;*.bmp;*.gif;*.png)|*.jpg;*.bmp;*.gif;*.png;*.ico")
                .RestoreDirectory = True
                .Multiselect = False
                .CheckFileExists = True
                If .ShowDialog(Frm) = DialogResult.OK Then
                    ChosenBitmap = Bitmap.FromFile(.FileName)
                Else
                    ChosenBitmap = _ErrBitmap
                End If
            End With
        End Using
        Return ChosenBitmap
    End Function
End Module

Code to use in any Form, PictureBox Click Event

Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
    PictureBox1.Image = Module1._GetImgOFD(Me, PictureBox1)
End Sub

Upvotes: 0

Amarbir
Amarbir

Reputation: 23

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        OpenFileDialog1.Title = "Please select a file"
        OpenFileDialog1.InitialDirectory = "c:"
        OpenFileDialog1.ShowDialog()
        PictureBox1.ImageLocation = OpenFileDialog1.FileName.ToString
        PictureBox1.Visible = True


    End Sub

Upvotes: 0

bto.rdz
bto.rdz

Reputation: 6720

try this:

photo = image.Fromfile( OpenFileDialog1.FileName)

Hope it helps

Upvotes: 2

Jens
Jens

Reputation: 6375

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

Upvotes: 4

Related Questions