User6996
User6996

Reputation: 3003

Image Resources in WinForms

I want to store images in a .dll file and use them for my winform application. but Im not able to load .dll content

   System.IO.Stream stream;
   System.Reflection.Assembly assembly;
   Image bitmap;
   assembly = System.Reflection.Assembly.LoadFrom(Application.ExecutablePath);
   stream = assembly.GetManifestResourceStream("global::template.Properties.Resources.test.png");
   bitmap = Image.FromStream(stream);
   this.background.titleImage = bitmap; // image box

Upvotes: 1

Views: 2214

Answers (2)

Tim Lloyd
Tim Lloyd

Reputation: 38434

The most likely problem is that the path you are specifying in GetManifestResourceStream() is not correct.

I find the following helpful to debug these issues:

  1. Open assembly that has the resource embedded into it, in Reflector.
  2. Open the Resources folder.
  3. From the list of resource paths\folders, find the one with your resource in it.
  4. If you cannot find your resource, did you mark it as an embedded resource?
  5. Your resource path will be "Resource folder" + "." + "resource name".

e.g. "PrismDemo.Properties.Resources.resources.app.ico"

Upvotes: 3

Hans Passant
Hans Passant

Reputation: 941218

You are using the wrong name, "global::" doesn't appear in the manifest resource name. A typical name would be project.test.png if you added the resource to the project and set its Build Action to "Embedded Resource". To avoid guessing at the name, use Ildasm.exe and find the .mresource in the manifest.

However, your name suggests you added the resource in the Project + Properties, Resources tab. Good idea, that auto-generates a property name for the resource. You'd use project.Properties.Resources.test to reference it. You'll get an Image back, no need to use GetManifestResourceStream.

The project name I listed above is the default namespace name you assigned to the project. Project + Properties, Application tab, Default namespace setting.

Upvotes: 2

Related Questions