Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Guyz,

I'm having a hard time for displaying my images in a image object..

i saved my images in in a folder, heres the path where im saving my images.

C:\Users\Documents\Visual Studio 2010\Projects\ProjectItems\ProjectItems\CouponLogo

actually the folder was inside my project in c#

i have a image object in asp.net, but when i locate the path of picture it doesnt work

heres my code:

C#
string startupPath = AppDomain.CurrentDomain.BaseDirectory;
string targetPath = startupPath + "CouponLogo\\";
imgLogo.ImageUrl = startupPath + smile.png


thanks in advance..
Posted
Updated 22-Feb-17 17:42pm

hi

string startupPath = AppDomain.CurrentDomain.BaseDirectory;
string targetPath = startupPath + "CouponLogo\\"
Image1.ImageUrl = targetPath + "Blue hills.jpg"---this will not work

becuse this url is big...

replace of this code you can use like this it is coming for me
Image2.ImageUrl = @"~\CouponLogo\Blue hills.jpg"




thanks
naresh.G
 
Share this answer
 
This code is easy to check up under the debugger. You should always use the debugger is such cases. You could compare the file name you calculated with what you expect. If you see the difference and don't understand you, it would make a good correct question for CodeProject.

Two recommendations for you:

The most universal way of calculating executable directory is this:
C#
string exePath =
    System.IO.Path.GetDirectoryName(
        System.Reflection.Assembly.GetEntryAssembly().Location));


Please try. Some other methods depends on where you host the code, in application, service, Visual Studio runtime host, etc.

Now, as you place your image in the executable directory, it means you use it read-only. I suggest you use resources instead.

Don't add an image to your directory. Create a .resx resource. Edit it by using "Add resource" => "Add existing file". Add a file. If a file was not in the same directory with the resource file, it will be copied there, so be careful not to keep multiple copies of the same file. Also, this file will be added to the project structure, and its reference will be added to your resource. Open the property of the project node representing your image file. You will see that it is not to be copied in output of the build. It will be places in resource embedded in your executable file. You will see that Visual Studio auto-generated code file, in a child node of the node representing the resource file. Open this file and locate a static class and its static property with the name resembling the name of your image file you used on input. Use this property to access the resource; don't use resource manager and string name of the resource — this is not really supportable way.

Use the property. If, for example, your image file is PNG, the property will be of the type System.Drawing.Bitmap. You won't need to match resource name, read image data from resource stream, etc. Just use the bitmap instance.

One more benefit: it will protect your images from modification by the user. If you also sign your assembly it would make the modification of your images nearly impossible.

—SA
 
Share this answer
 
v3
Shouldn't smile.png be in quotation marks as in

C#
<code>
    imgLogo.ImageUrl = startupPath + "smile.png";
</code>


with a semicolon following?

Gus
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Aug-11 22:30pm    
Good catch, but this is not OP's problem, otherwise it would not compile, but OP's code does compile but does not work as expected.
Please see my solution.
--SA
Curtis Smithson 29-Oct-15 7:48am    
Wouldn't you be adding "smile.png" as a string to the startupPath?
gggustafson 29-Oct-15 11:19am    
Yes. But the result is stored in imgLogo.ImageUrl

Are you in WinForms or WebForms?
Curtis Smithson 19-Nov-15 12:15pm    
Yeah I understand what you did now..Done some work on that are recently

And WinForms..Always WinForms with me lol
C#
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames()

will give you an array of paths (string[]) to all your embedded resources.

to get a stream for an embedded resource file called smile.png located in a folder called MyResources:
C#
Assembly.GetExecutingAssembly().GetManifestResourceStream("AssemblyName.MyResources.smile.png");
 
Share this answer
 
v2
Comments
Member 8092191 25-Aug-11 21:57pm    
it's not working...i dont know if im correct in using it heres what i did:

imgLogo.ImageUrl = Assembly.GetExecutingAssembly().GetManifestResourceStream("AssemblyName.CouponLogo." + filename + "").ToString();
Sergey Alexandrovich Kryukov 25-Aug-11 22:28pm    
There is much more maintainable way to use resources for image files. Please see my solution.
--SA

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