1,105,380 Community Members

image application path.

Member Avatar
lishannx
Light Poster
29 posts since Dec 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

ive created a new picturebox with the image to be directed from my directory.

but it came be the case that the directory path will always be on my desktop.

how do i make the path not hard-coded?

pictureBox = new PictureBox();
pictureBox.Image = Image.FromFile(@"D:\Documents and Settings\12344\Desktop\Model Products 40x40 v2\Model Products 40x40\CCTV\CCTV1Button.jpg");


this shld not be the case. it shld not be hard-coded. the directory might not be in the place where i start the program.

could someone help and tell me how to set the directory path name when i had already pasted the whole image folder into my program's directory?


thank u. appreciated.

Member Avatar
Diamonddrake
Master Poster
721 posts since Mar 2008
Reputation Points: 396 [?]
Q&As Helped to Solve: 92 [?]
Skill Endorsements: 8 [?]
 
0
 

there are quite a few ways to do this, simplest

string path =  Application.StartupPath;
//This gets the directory the application was started from.

And more complicated, but many people will tell you this is the only correct way, I argue against that. but it certainly does work also.

string correctPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
Member Avatar
lishannx
Light Poster
29 posts since Dec 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

okay, but how do u direct to a particular jpeg in the directory ?

Member Avatar
Diamonddrake
Master Poster
721 posts since Mar 2008
Reputation Points: 396 [?]
Q&As Helped to Solve: 92 [?]
Skill Endorsements: 8 [?]
 
1
 

*sigh*

string path =  Application.StartupPath;
string subfolder = @"\Images\";
string imagefilename = "myjpeg.jpg";

pictureBox.Image = Image.FromFile(path + subfolder + imagefilename);
Question Answered as of 4 Years Ago by Diamonddrake
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article