![](/img/trans.png)
[英]Can't load a manifest resource with 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捕獲和顯示的異常如下:
'null'的值對'stream'無效。
在代碼執行期間觀察我的變量時,我注意到了某種方式
Stream s = _assembly.GetManifestResourceStream(string.Format("{0}{1}.png", this.kaartWaarde, this.figuur.Substring(0, 1)));
即使使用Kaarten.{0}{1}.png
格式,也無法找到圖像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
要獲取資源,您應該使用GetManifestResourceStream(string)
和區分大小寫的限定資源名稱,該名稱由兩個點分隔的部分組成:
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);
哪個Image
類提供了一個接受流的構造函數?
猜猜你可以使用System.Drawing.Image.FromStream
並將其System.Drawing.Bitmap
為System.Drawing.Bitmap
。 然后你可以使用以下內容:
System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
_assembly.GetManifestResourceStream("{ProjectName}.resources.{ImageName.ext}");
這將正確加載圖像。
資源!=資源,這將導致它出錯。
解決此問題的最簡單方法是獲取嵌入資源名稱的列表:
string[] result = myAssembly.GetManifestResourceNames();
在單元測試中運行它,或創建一個引用程序集的單獨控制台應用程序,執行typeof()。Assembly.GetManifestResourceNames()。
請記住在每個文件上指定構建操作:嵌入式資源並至少構建一次程序集以嵌入文件。
我不確定,但我認為你應該使用的文件名遵循(?
我想以強類型的方式從Properties.Resources訪問我的圖像。
要實現此目的,請右鍵單擊項目根目錄並選擇“屬性”,然后打開“資源”選項卡並將圖像拖放到此處(可以從項目的名為resources的文件夾中選擇它們)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.