Willie Mwewa
Willie Mwewa

Reputation: 361

How can i add images to a picturebox using VB?

I want to create an image viewer that will load an unlimited number images to a form. Could someone please help me with this? Here is the code. there is a button named loadImagesBttn and the picture box named myImage

Class MainWindow 

    Private Sub loadImagesBttn_Click(sender As Object, e As RoutedEventArgs) Handles loadImagesBttn.Click
        Dim dlg As New Microsoft.Win32.OpenFileDialog()
        dlg.FileName = "Image" 'Default image file name
        dlg.DefaultExt = ".jpg" 'Default extention 
        dlg.Filter = "Images (.jpg)| *.jpg " 'Filter images my extension

        'Show open file dialog box 
        Dim result? As Boolean = dlg.ShowDialog()

        'Process open file dialog box results 
        If result = True Then
            'Open image 
            Dim selectedImage As String = dlg.FileName

            myImage.Source = 'this is where I get confused.

        End If
    End Sub

End Class

Upvotes: 0

Views: 2246

Answers (1)

Alex K.
Alex K.

Reputation: 175776

You need to load the file:

myImage.Source = New BitmapImage(New Uri(selectedImage))

Upvotes: 1

Related Questions