Picture box add image

kiwis

Member
Joined
Apr 23, 2007
Messages
7
Programming Experience
Beginner
my project has a picture box. How can I add a image to it by using the file location???
 
Hello,

I have done this two ways shown below to use the picture logo.jpg. The first is what I use now, and it does not tie up the image. If you use the second, then later you want to replace the image, you will get an error.

VB.NET:
PictureBox.Image = Image.FromStream(New IO.MemoryStream(My.Computer.FileSystem.ReadAllBytes("logo.jpg")))

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] image [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Image = Bitmap.FromFile([/SIZE][SIZE=2][COLOR=#800000]"logo.jpg"[/COLOR][/SIZE][SIZE=2])
PictureBox1.Image = image
[/SIZE]

Now, if you need to locate the image at runtime, you can use this.

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] OpenFileDialog [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] OpenFileDialog = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] OpenFileDialog
OpenFileDialog.ShowDialog()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Picture [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = OpenFileDialog.FileName[/SIZE][SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] I [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Image = Image.FromStream([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] IO.MemoryStream([/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Computer.FileSystem.ReadAllBytes(Picture)))
PictureBox.Image = I
[/SIZE]

Hope this helps,
Chris
 
Back
Top