开发者

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


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"


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜