How to load a image to picturebox?

aspfun

Active member
Joined
Feb 16, 2010
Messages
34
Programming Experience
5-10
I code to load an image below. It works fine.

Picturebox1.Image = Image.FromFile("c:\logo.gif", True)

But, code below do not work. What is wrong?
(images is a subfolder under app Order)

Picturebox1.Image = Image.FromFile(".\images\logo.gif", True)
 
Thread moved, please post in an appropriate forum.
VB.NET:
Picturebox1.Image = Image.FromFile(Path.Combine(Application.StartupPath, "images\logo.gif"), True)
 
I tried it but do not work, it point to a file in "D:\Myapp\Order\Order\bin\Debug\images\logo.gif"

I need to point to

"D:\Myapp\Order\images\logo.gif"
 
Resources

Why not add your image to the Resources file? That way you're not going to have any path issues!

PictureBox1.Image = My.Resources.logo.gif
 
aspfun, if your app is running in the 'D:\Myapp\Order\Order\bin\Debug\' directory, then
VB.NET:
Path.Combine(Application.StartupPath, "images\logo.gif")
Will produce this path for you:
VB.NET:
D:\Myapp\Order\Order\bin\Debug\images\logo.gif
Why not add your image to the Resources file? That way you're not going to have any path issues!

PictureBox1.Image = My.Resources.logo.gif
This is only valid if you're using VS 2005 or newer, he's using (according to his forum profile) VS 2002 which doesn't have the My namespace nor the Resources abilities.
 
I tried PictureBox1.Image = My.Resources.logo, it works fine.
(I just updated my profile. I am using vs2008)
But I am still looking for a code to read subfolder info.
 
For example, for Order application, here is physical path in my pc:
d:\myapp\Order. There are some subfolder under project name Order:

Images (store some image files)
App_Data (store database file, such as order.mdb)
Reports (store Crystal repors)

I need a way to open any file in subfolders. In asp.net, it is easier to code like:
"~\images\logo.gif" but in windows app, I do not know how.
 
Add this to your form load event:
VB.NET:
Messagebox.Show(Application.StartupPath)
What path is displayed?

I'm assuming that your app is in "D:\myapp\Order", but I just want to make sure.
 
I just tested it using a code below

Label1.Text = Application.StartupPath

It will display d:\myapp\order\order\bin\debug
 
ah, ok this makes sense now. On each image in the images folder in in your VS Solution Explorer, set the 'Copy to Output Directory' to 'Copy if newer'. What this will do is create a d:\myapp\order\order\bin\debug\images folder (with your images) everytime you run your app then use:
VB.NET:
Path.Combine(Application.StartupPath, "\images\ImageName.gif")

What's happening is the images are in the parent directory: D:\myapp\Order and you need them to be in a sub directory of where the app is, which is: d:\myapp\order\order\bin\debug. WinForms programming is totally different than webforms as in when you have images that need to be deployed with the app, you need to configure them to be so. By setting the 'Copy to output dir' property, you're telling VS that when you compile your app (whether in debug or release mode, which are two different file locations mind you) that those images need to be in a subfolder of the exe respectively.
 
Back
Top