使用GetManifestResourceStream查找嵌入式

使用GetManifestResourceStream查找嵌入式

本文介绍了无法使用GetManifestResourceStream查找嵌入式资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理Card DLL,在其中我需要将每个卡的图像文件作为嵌入式资源,当前项目如下所示:

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:

注意:卡片图像(.png)位于资源"文件夹中.

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

我似乎得到的唯一例外是创建 Kaart 对象的例外,其代码如下:

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

被捕获的异常&使用MessageBox显示的内容如下:

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

在代码执行过程中观察变量时,我以某种方式注意到了这一行

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

即使使用 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?

现在可以正确加载图像了,但是ImageSourceConverter仍然抛出 NullReferenceException

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

创建卡片和据我所知,加载 Stream 对象(并将它们卸载到 Image 对象)现在可以正常工作,但是当尝试在WPF图像中实际显示图像时如果使用下面的代码进行控制,则会抛出NullRefException.

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

推荐答案

哪个 Image 类提供了一个接受流的构造函数?

Which Image class provides a constructor that accepts a stream?

猜猜您可以使用 System.Drawing.Image.FromStream 并将其转换为 System.Drawing.Bitmap .然后,您可以使用以下内容:

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

这篇关于无法使用GetManifestResourceStream查找嵌入式资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 07:07