Can't find embedded resource using GetManifestResourceStream

一笑奈何 提交于 2021-02-08 13:19:12

问题


I'm currently working on a Card DLL in which I would need the image files for each card as an embedded resource, the current project looks like this:

Card images are in the Resources folder

Note: The card images (.png) are in the Resources folder.

The code I've been trying & is pretty much the only code I can find, is this:

Assembly _assembly;
Stream _imageStream;

private Image img;
public Image Img
{ 
get
    {
        return img;
    }
}

public Kaart() : this(5, "Spades") { }

public Kaart(int val, string s)
{
    this.KaartWaarde = val;
    this.Figuur = s;
    this._imageStream = imgS();
    this.img = new Image(_imageStream);
}

private Stream imgS()
{
    try
    {
        _assembly = Assembly.GetExecutingAssembly();
        Stream s = _assembly.GetManifestResourceStream(string.Format("{0}{1}.png", this.kaartWaarde, this.figuur.Substring(0, 1)));
        if (s != null)
            return s;
        else
            return null ;
    }
    catch
    {
        throw new Exception("Kaart kan niet geladen worden.");
    }
}

The only exception I seem to get is an exception in the creation of the Kaart object for which the code is here:

Kaart k = null;
try
{
    k = new Kaart();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message); //This MessageBox is being shown
}
ImageSourceConverter c = new ImageSourceConverter();
testBox.Source = (ImageSource)c.ConvertFrom(k.Img);

The exception being caught & shown by use of MessageBox is as follows:

Value of 'null' is not valid for 'stream'.

While watching my variables during code execution, I noticed somehow the line

Stream s = _assembly.GetManifestResourceStream(string.Format("{0}{1}.png", this.kaartWaarde, this.figuur.Substring(0, 1)));

Fails to find the image, even when using the format Kaarten.{0}{1}.png

This simply makes me wonder if I'm doing something wrong here or if I'm using the wrong syntax. Any ideas?

EDIT: Images are now being loaded properly, however the ImageSourceConverter is still throwing a NullReferenceException

Card creation & loading the Stream objects (and unloading them into an Image object) are working fine now as far as I can tell, however when trying to actually show the images in my WPF Image control using the code below, the NullRefException is thrown.

Kaartspel kaarten = new Kaartspel();

Kaart k = kaarten.kaarten[7];

ImageSourceConverter c = new ImageSourceConverter();
testBox.Source = (ImageSource)c.ConvertFrom(k.Img); //Exception here

回答1:


Which Image class provides a constructor that accepts a stream?

Guess you could use System.Drawing.Image.FromStream and cast it to System.Drawing.Bitmap. Then you can use the following:

System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());




回答2:


To get a resource you should use GetManifestResourceStream(string) with the case-sensitive qualified resource name which consists of two dot separated parts:

  1. The default namespace of the project which contains the resource. In most cases the default namespace equals to the project name.
  2. The resource file name including the relative path to the project. All directory delimiters must be replaced by dots.
resource_name ::= default_namespace '.' full_file_name
full_file_name ::= (directory_name '.')* file_name

In your case:

string name = string.Format("Kaarten.Resources.{0}{1}.png", this.kaartWaarde, this.figuur.Substring(0, 1)));
Stream stream = _assembly.GetManifestResourceStream(name);

Another way to get a resource is using GetManifestResourceStream(Type, string). In this case the name is relative to the specified type's namespace (MSDN, see Remarks).

string name = string.Format("Resources.{0}{1}.png", this.kaartWaarde, this.figuur.Substring(0, 1)));
Stream stream = _assembly.GetManifestResourceStream(typeof(Kaart), name);



回答3:


_assembly.GetManifestResourceStream("{ProjectName}.resources.{ImageName.ext}");

This will load the image correctly.

Resources != resources this will cause it to error.




回答4:


The easiest way to resolve this is to fetch a list of the embedded resource names:

string[] result = myAssembly.GetManifestResourceNames();

Run that in a unit test, or create a separate console application that references the assembly, do a typeof().Assembly.GetManifestResourceNames().

Remember to specify on each file Build Action: Embedded Resource and build the assembly at least once to embed the files.

I'm not to sure, but I think the file names you should use follow the pattern of (?.


回答5:


I'd like to access my images from Properties.Resources in a strong-typed way.

To achieve this right-click on your project root and select Properties then open Resources tab and drag&drop your images here (you can select them from you project's folder called resources).



来源:https://stackoverflow.com/questions/30006530/cant-find-embedded-resource-using-getmanifestresourcestream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!