Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I added an image in resource file and read it. It was easy.
But when i changed the build action to content and then tried to read it, gave me exception.

C#
var rm = new ResourceManager("Resource1", System.Reflection.Assembly.GetExecutingAssembly());
pictureBox1.Image = (Bitmap)rm.GetObject("excel");


does any one has an idea on this.
Posted
Comments
Sergey Alexandrovich Kryukov 27-Apr-11 0:58am    
Tag it! Language, platform, version... How an expert can pay attention for the question of interest?
--SA

Typically, in practice, this is much more easy than that. You usually don't need to write any code.

Let's take your bitmap. Create an new .resx file, open it, go to the menu on top and click "Resources"... "Add existing file". Browse and load the bitmap file, say "MyPicture.png". It will add a copy of "MyPicture.png" to your project and a link to this file in your resource.

You resource will create two nodes in solution explorer: .resx file and its child node with auto-generated .cs file. Open this file, locate a static class and its static property with the name close to the name of your bitmap file, something like "MyPicture". This is your bitmap, a static property you should use to access your bitmap in your resource.

Use it!
Don't use ResourceManager or anything like that — it is already done for you!

[EDIT]

In connection to discussion:

If you need to read a nearly permanent file (like a resource, something not modified during run time), place it in executable directory or a relative path relative to the executable directory. Here is how to calculate it:

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


—SA
 
Share this answer
 
v2
Comments
nitin_ion 27-Apr-11 1:11am    
I tried it also but when i refer it

pictureBox1.Image = Resource1.excel;

it gives error

"Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "WindowsFormsApplication1.Resource1.resources" was correctly embedded or linked into assembly"

My resource file is content [build action] and public [access modifier]
Sergey Alexandrovich Kryukov 27-Apr-11 1:29am    
Something is broken, I don't know what. Did you make sure your source bitmap file (what is it, excel.png?) is a valid bitmap showing correctly with some viewer?

When I do it from scratch it always works right away.
--SA
Abhinav S 27-Apr-11 1:43am    
Yeah. Different approach. 5.
Sergey Alexandrovich Kryukov 27-Apr-11 3:17am    
Thank you, Abhinav.
I just figured out OP does not need any embedded resources at all. Just a bad confusion. Please see below. Not my fault. (Sigh...)
--SA
nitin_ion 27-Apr-11 1:44am    
Yes image is bitmap and it does show it in image editors. If you have a sample code can you send me.
My purpose for this is to update resource files only instead of updating the exe everytime.
A file setup for build action Content as shown below will not be included in the assembly. As a result whe you try to load the file using the resource manager you get an error. You might want to check out msdn for more details on build actions.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Apr-11 1:07am    
I think it needs much more simple approach.
Please see my answer.
--SA
Abhinav S 27-Apr-11 1:43am    
I just explained to him why he gets the error :).
Tarun.K.S 27-Apr-11 14:49pm    
Correct.
I tried reading it as xml and it did worked

C#
var x = new XmlDocument();
               XmlNode rootNode;
               x.Load("C:\\Users\\njain\\Documents\\Visual Studio 2008\\Projects\\WindowsFormsApplication1\\WindowsFormsApplication1\\Resource1.resx");
               rootNode = x.SelectSingleNode("root");
               foreach (XmlNode Node in rootNode)
               {
                   if ((Node.Name == "data") && Node.Attributes["name"].Value == "excel")
                   {
                       var cc = Node.LastChild.LastChild.Value;
                       var c=cc.Split(';');
                   }
               }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Apr-11 1:07am    
Why?!

I think it needs much more simple approach.
Please see my answer.
--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