Click here to Skip to main content
14,300,703 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi guys.

I have a bunch of pictures in my resources folder, and foreach image in the resources folder i create a button and set its background image to the image of the foreach loop.

Then on button click i pass the image thats on the button to a method, but i need the file path of the IMage.

How do i get that file path from the image object?

Please help.

Thanks in advance.
Why? Why do you think there is a path?
Amir Mahfoozi 5-Feb-12 5:27am
The answer depends on the application type. ASP.NET ? WPF ? Windows Forms ?
In a way, it does not matter much. This is resource (OP indirectly confirmed it by accepting my answer) and as such can further be used in different application types.
Amir Mahfoozi 5-Feb-12 23:59pm
I said that because I saw different approaches for loading images in resources in WPF and ASP.NET. Anyway thanks for your consideration Sergey :)

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

No, you don't need a file path for any images! Such path don't even exist. You use resources, so your image will be read from the resource string, not from an external file.

If you are using .res resources, you are already doing it in a write way: you add each image file to your resource separately, which is much better for maintenance than using image editor in Visual Studio. But the thing is: you image directory exists only in your project, it does not exists in your output directory, for a good reason: all files referenced in some resource file of your project are marked as "do not copy" in the properties. They should not be copied because they are embedded is the assembly's executable module when the project is built.

But you don't even need to use resource stream or anything to read the resource.

Everything is already done for you. Look at the tree node in the Solution Explorer tree view, a child node of a resource file node. Visual Studio auto-generate easy-to-use source code for you. Open this file in the editor. You will find a static class with some static properties with the name close to the names of your source files with images. The type if the properties is already Image. Those properties return instanced of the images which are already prepared and ready for use.

Just use them in your code.

NeptuneHACK! 5-Feb-12 4:08am
moi 5 ^_^
Thank you. What language is that?
Member 8102934 5-Feb-12 15:35pm
Thanks for the response! Much appreciated!
You are very welcome and probably have all you need.

Good luck, call again.

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100