无法使用 GetManifestResourceStream 找到嵌入式资源

Can't find embedded resource using GetManifestResourceStream

我目前正在开发一个卡片 DLL,我需要将每张卡片的图像文件作为嵌入式资源,当前项目如下所示:

注意:卡片图像 (.png) 在 Resources 文件夹中。

我一直在尝试的代码几乎是我能找到的唯一代码,是这样的:

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 对象时的例外,其代码在此处:

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捕获并显示的异常如下:

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

在代码执行期间观察我的变量时,我以某种方式注意到了行

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

找不到图像,即使使用 Kaarten.{0}{1}.png

格式也是如此

这让我怀疑我是不是这里做错了什么,或者我是否使用了错误的语法。有什么想法吗?

编辑: 图像现在正在正确加载,但是 ImageSourceConverter 仍然抛出 NullReferenceException

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

Kaartspel kaarten = new Kaartspel();

Kaart k = kaarten.kaarten[7];

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

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

这将正确加载图像。

Resources != resources 这会导致它出错。

要获取资源,您应该使用 GetManifestResourceStream(string) 和区分大小写的合格资源名称,该名称由两个点分隔的部分组成:

  1. 包含资源的项目的默认命名空间。在大多数情况下,默认命名空间等于项目名称。
  2. 资源文件名,包括项目的相对路径。所有目录分隔符都必须用点代替。
resource_name ::= default_namespace '.' full_file_name
full_file_name ::= (directory_name '.')* file_name

你的情况:

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

另一种获取资源的方法是使用 GetManifestResourceStream(Type, string)。在这种情况下,名称是相对于指定类型的命名空间的(MSDN,请参阅备注)。

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

解决此问题的最简单方法是获取嵌入资源名称的列表:

string[] result = myAssembly.GetManifestResourceNames();

运行 在单元测试中,或创建一个引用程序集的单独控制台应用程序,执行 typeof()。Assembly.GetManifestResourceNames()。

记得在每个文件上指定 构建操作:嵌入资源 并至少构建程序集一次以嵌入文件。

我不确定,但我认为您应该使用的文件名遵循 (?.

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

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

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

我想以强类型方式从 Properties.Resources 访问我的图像。

要实现此目的,请右键单击您的项目根目录和 select 属性,然后打开“资源”选项卡并将您的图像拖放到此处(您可以 select 从项目的资源文件夹中选择它们)。