[英]Can't find embedded resource using GetManifestResourceStream
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: 我目前正在开发一个卡DLL,我需要将每张卡的图像文件作为嵌入式资源,当前项目如下所示:
Note: The card images (.png) are in the Resources folder. 注意:卡片图像(.png)位于Resources文件夹中。
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
对象时的异常,代码在这里:
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: 使用MessageBox捕获和显示的异常如下:
Value of 'null' is not valid for 'stream'. 'null'的值对'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
即使使用Kaarten.{0}{1}.png
格式,也无法找到图像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
编辑:图像现在正在正确加载,但ImageSourceConverter仍然抛出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. 卡片创建和加载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
To get a resource you should use GetManifestResourceStream(string)
with the case-sensitive qualified resource name which consists of two dot separated parts: 要获取资源,您应该使用GetManifestResourceStream(string)
和区分大小写的限定资源名称,该名称由两个点分隔的部分组成:
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)
. 获取资源的另一种方法是使用GetManifestResourceStream(Type, string)
。 In this case the name is relative to the specified type's namespace ( MSDN , see Remarks). 在这种情况下,名称是相对于指定类型的命名空间( MSDN ,请参阅备注)。
string name = string.Format("Resources.{0}{1}.png", this.kaartWaarde, this.figuur.Substring(0, 1)));
Stream stream = _assembly.GetManifestResourceStream(typeof(Kaart), name);
Which Image
class provides a constructor that accepts a stream? 哪个Image
类提供了一个接受流的构造函数?
Guess you could use System.Drawing.Image.FromStream
and cast it to System.Drawing.Bitmap
. 猜猜你可以使用System.Drawing.Image.FromStream
并将其System.Drawing.Bitmap
为System.Drawing.Bitmap
。 Then you can use the following: 然后你可以使用以下内容:
System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
_assembly.GetManifestResourceStream("{ProjectName}.resources.{ImageName.ext}");
This will load the image correctly. 这将正确加载图像。
Resources != resources this will cause it to error. 资源!=资源,这将导致它出错。
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(). 在单元测试中运行它,或创建一个引用程序集的单独控制台应用程序,执行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 (?. 我不确定,但我认为你应该使用的文件名遵循(?
I'd like to access my images from Properties.Resources in a strong-typed way. 我想以强类型的方式从Properties.Resources访问我的图像。
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). 要实现此目的,请右键单击项目根目录并选择“属性”,然后打开“资源”选项卡并将图像拖放到此处(可以从项目的名为resources的文件夹中选择它们)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.