簡體   English   中英

使用GetManifestResourceStream找不到嵌入式資源

[英]Can't find embedded resource using GetManifestResourceStream

我目前正在開發一個卡DLL,我需要將每張卡的圖像文件作為嵌入式資源,當前項目如下所示:

卡片圖像位於Resources文件夾中

注意:卡片圖像(.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)和區分大小寫的限定資源名稱,該名稱由兩個點分隔的部分組成:

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

哪個Image類提供了一個接受流的構造函數?

猜猜你可以使用System.Drawing.Image.FromStream並將其System.Drawing.BitmapSystem.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM