Rev
Rev

Reputation: 2265

retrieve resource values programmatically in Windows application

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().

Upvotes: 1

Views: 2157

Answers (3)

Rev
Rev

Reputation: 2265

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");

Upvotes: 2

p.campbell
p.campbell

Reputation: 100567

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;

Upvotes: 1

Chris Taylor
Chris Taylor

Reputation: 53699

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

Upvotes: 3

Related Questions