tags:

views:

43

answers:

3

I want to get a specific value from a resource programmatically. For example, I have a FileName variable which contains the resource Image name.

How to get that resource with this variable?

I try this but not working: (file is null after line 3)

public Bitmap FindImgaeFromResource(string ImageFileName)
{
    Assembly thisExe;
    thisExe = Assembly.GetExecutingAssembly();
    Stream file = thisExe.GetManifestResourceStream(ImageFileName);
    return (Bitmap)Image.FromStream(file);
 }

This is not WPF application, and I can't use this.tryfindresource().

+3  A: 

My guess would be that the name you are using is not correct. If you open the assembly with ILDASM you can find the correct name for the resource.

Here is a KB article on loading images from the assembly resources.

http://support.microsoft.com/kb/324567

Chris Taylor
cool but there is other way with many advantages.
Rev
@Rev, it would be good for future readers if you stated all the advantages in your answer below.
Chris Taylor
@Chris, So let me explain: My solution and your solution is two way to access to Resource Object(almost Same in access and I don't think one of them better than other one), but there is Big different between our solutions(mean Resource-manager). Resource-manger is a great way to work with Resource. also do many things with resource like create ,read or others action. so my answer is not better than your answer(Just different way) but I Presentation a tool with many advantages can use for many problems!
Rev
@Chris, one vote for your answer and one vote for your good question. i can decide which one is better answer, so if you want call bounty :)
Rev
@Rev, both solutions are your solution. Your questions has solution 1, your answer has solution 2. My answer is just gives you more detail around the solution you presented in your question. The question is, was your problem that the name you used for the resource was causing your load problem?
Chris Taylor
@chris, Absolutely my problem is how access to objects in resource. not that code i wrote in my question! so that way have more problem finding real object(use ILDASM ). In my solution if i write "obj1" which is name of resource object, i can find that. but in that code(in my question) i cant find "obj1" and get null value. you see that is disadvantage of your solution (real name different from obj1). And about your answer, your answer is correct way to use that code, so your answer is complete way(with detail) to use that code.
Rev
+1  A: 

Try modifying this set of statements.

string fileName ="AppNameSpace.FolderWithImage.logo.png";

Assembly assembly = Assembly.LoadFrom(Application.ExecutablePath);
Stream stream = assembly.GetManifestResourceStream();
Image bitmap = Image.FromStream(stream);

You could also try to reference the resource directly. This doesn't exactly help you with the circumstances in the question, but it's less prone to errors when you can use it.

byte[] imgBytes = Properties.Resources.MyImageFile;
p.campbell
If thisExe is null the op would get an exception on line 3.
Chris Taylor
@Chris: you're correct....thanks!
p.campbell
+1  A: 

There is simplest way :

Use ResourceManager

We should create new object Like this:

ResourceManager rManger = new ResourceManager("ProjectName.Properties.Resources", typeof(Resources).Assembly);

then Use this codes to retrive Objects or Strings:

(System.Drawing.Bitmap)rManger.GetObject("Resource-Name");

or

 rManger.GetString("Resource-Name");
Rev