Displaying image from folder/file in vb.net
Asked Answered
W

2

9
Dim ImagePath As String = "images/spaceship2.png"
Dim img1 As Bitmap
Dim newImage As Image = Image.FromFile("images/spaceship2.png")

img1 = New Bitmap(ImagePath)
pb2.ImageLocation = ImagePath

pb1.Image = newImage

I want to display image from folder, for example, student with an id number of 22137471, the picture with the name of 22137471 will be display on my picture box, between, i saw this code somewhere in google.

Winner answered 3/11, 2013 at 1:22 Comment(0)
S
15

i want to display image from folder, for example, student with an id number of 22137471, the picture with the name of 22137471 will be display on my picture box

Try something like...

Dim id As String = "22137471"
Dim folder As String = "c:\some path\folder"
Dim filename As String = System.IO.Path.Combine(folder, id & ".png")
PictureBox1.Image = Image.FromFile(filename)

Here's an updated version that doesn't lock the original image file:

Dim id As String = "22137471"
Dim folder As String = "c:\some path\folder"
Dim filename As String = System.IO.Path.Combine(folder, id & ".png")
Try
    Using fs As New System.IO.FileStream(filename, IO.FileMode.Open)
        PictureBox1.Image = New Bitmap(Image.FromStream(fs))
    End Using
Catch ex As Exception
    Dim msg As String = "Filename: " & filename &
        Environment.NewLine & Environment.NewLine &
        "Exception: " & ex.ToString
    MessageBox.Show(msg, "Error Opening Image File")
End Try
Sallee answered 3/11, 2013 at 2:6 Comment(1)
Dim id As String = LinkLabel1.Text Dim folder As String = "d:\image\" Dim filename As String = System.IO.Path.Combine(folder, id & ".jpg") PictureBox1.Image = Image.FromFile(filename) 'it is not working, i will load it from datadisk d, my folder is image, inside it is the picture that will be displayWinner
P
0

You can try this:

PictureBox1.Image = Image.FromFile("c:\some path\folder\myImage.jpg")
Pithos answered 30/8, 2015 at 15:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.