2013-01-18 60 views
0

我目前正在創建遊戲引擎,並需要從引用我的庫的客戶端程序集中的資源加載圖像。我正在使用此代碼。從客戶端程序集加載資源

public static Image LoadImageFromResource(string name){ 
    string asmname = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location).Replace('\\', '.').Replace('/', '.'); 
    MessageBox.Show(asmname); 
    MessageBox.Show(asmname + "." + name.Replace('\\', '.').Replace('/', '.')); 
/*164*/ return (Image)new Bitmap(Assembly.GetEntryAssembly().GetManifestResourceStream(asmname + "." + name.Replace('\\', '.').Replace('/', '.'))); 
} 

而且我從客戶端測試GECS_TEST.exe

/*11*/ Image img = Game.LoadImageFromResource("mario_left.png"); 

調用此這些都對MessageBox

GECS_TEST輸出

GECS_TEST.mario_left.png

而且我得到這個例外

System.ArgumentException: Value of 'null' is not valid for 'stream'. 
    at System.Drawing.Bitmap..ctor(Stream stream) 
    at GECS.Core.Game.LoadImageFromResource(String name) in C:\..\Game.cs:line 164 
    at GECS_TEST.Test.Main(String[] args) in c:\..\Test.cs:line 11 

感謝

+0

你的GetManifestResourceStream()似乎是空的......你用清單編譯你的項目嗎? –

+0

我已經將它設置爲'default manifest'。我已將資源複製到軟件包管理器中,並將其構建操作選爲嵌入式資源 –

+0

而且...如果您有GECS和GECS_TEXT ...爲什麼EntryAssembly指向GECS_TEST?改爲使用GetCallingAssembly。 –

回答