Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
I have one Picturebox in my windows application. I have one folder in solution explorer Folder name is Images.
have Following code

Bitmap bmp;
string ImagePath = (System.Environment.CurrentDirectory);
 if (ImagePath.EndsWith("\\bin\\Debug"))
 {
     ImagePath = ImagePath.Replace("\\bin\\Debug", "");
 }
ImagePath = ImagePath +"\\Images\\;
if (System.IO.File.Exists(ImagePath + dsReSchedule.Tables[0].Rows[0]["AIRLINECODE"].ToString() + ".jpg") == true)
 {
     bmp = new Bitmap(Image.FromFile(ImagePath + dsReSchedule.Tables[0].Rows[0]["AIRLINECODE"].ToString() + ".jpg"));
 }
 else
 {
     bmp = new Bitmap(Image.FromFile(ImagePath + "Noflight" + ".jpg"));
 }
 pbxImage.Image = bmp;




I Get in my datatable Flight image names I check that imagename is exist in The Folder "Images or not IF exists means I Got the Image.

But this coding is some problem. when making of setupfile Image is not loading
So

Is there any other way to Get Image please give me solution.
Posted

No wonder is does not work. I don't know where are you images and what is the directory you calculate in your code, but it cannot work in principle.

First, you use immediate constants of the type string everywhere. It is not supportable. These path names really belong in configuration files.

Now you calculate the image directory based on System.Environment.CurrentDirectory, then you find some sub-directory of it. This directory is actually a current directory which can be… anything at all. This is not a directory where you put your executable files (did you mean to assume that?); this is the directory where your user starts your application. It can be any valid directory. In one application start in could be one, next time it could be another.

The reliable way to find out executable directory is this:

C#
string exePath = System.IO.Path.GetDirectroryName(
    System.Reflection.Assembly.GetEntryAssembly().Location);


Now, are your image files read-only? You can use executable path reliable only if they are read-only. If not, you should use one of the "special directories" associated with the current user or all users. For this purpose, use System.Environment.GetFolderPath(System.Environment.SpecialFolder). Most typically, you will use the argument Environment.SpecialFolder.ApplicationData, Environment.SpecialFolder.CommonApplicationData or Environment.SpecialFolder.LocalApplicationData.

See:
http://msdn.microsoft.com/en-us/library/14tx8hby.aspx[^],
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx[^].

Let's come back to the situation when your image files are read-only. Even better method to use them would be embedding them in your application in the form of resources (.resx). Don't add them to the project directly. Create a .resx resource, use "Add resource" => "Add existing file". Add your image files. They will be added to the project, and references to them will be added to your resource. Look at the properties of each file. They won't be copied to output. Instead, they will be embedded to your executable. To use them, take a look at auto-generated source code file, as a child node of your .resx file. If has a static class with some static properties. The names of these properties will be based on the names of your original image files. You won't need to read resource stream, use resource manager or anything like that. Just use those properties immediately. If, for example, you image files were in PNG format (recommended) you auto-generated properties will be of the type Bitmap. Those bitmaps are ready-to-use.

Additional benefit is: your user won't be able to modify your images. If you also sign your assembly (highly recommended), you will make modification of those embedded images practically impossible. It beats you directory approach, doesn't it?

—SA
 
Share this answer
 
Comments
Simon Bang Terkildsen 6-Sep-11 22:02pm    
+5 Very thorough, as I've come to expect from you.
Sergey Alexandrovich Kryukov 6-Sep-11 22:39pm    
Thank you, Simon.
--SA
kranthi.oru 7-Sep-11 2:44am    
Hi SAKryukov My Images Folder in Solution Explorer.
Sergey Alexandrovich Kryukov 7-Sep-11 13:32pm    
So what? There is always a source file folder corresponding to this Solution folder in your "real" file system; during build it's mapped onto output path. All my advice is applicable.
--SA
Hi kranthi.oru

try this and put your image folder to solution's bin\debug directory as in windows application when we are debugging any application it runs from there and the current path for application is inside the bin\debug so put a copy in to that folder.
C#
Bitmap bmp;
string ImagePath = Application.ExecutablePath + @"\images\" + imagefilename


NOTE: while you deploying this application you do not need to put image directory to bin\debug folder.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900