您可以在資源循環..這樣的事情:
using System.Collections;
string image_name = "Desert";
foreach (DictionaryEntry kvp in Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true)) {
if ((string)kvp.Key == image_name) {
var bmp = kvp.Value as Bitmap;
if (bmp != null) {
// bmp is your image
}
}
}
你可以在一個不錯的小功能,把它包..像這樣的:
public Bitmap getResourceBitmapWithName(string image_name) {
foreach (DictionaryEntry kvp in Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true)) {
if ((string)kvp.Key == image_name) {
var bmp = kvp.Value as Bitmap;
if (bmp != null) {
return bmp;
}
}
}
return null;
}
用法:
var resourceBitmap = getResourceBitmapWithName("Desert");
if (resourceBitmap != null) {
pictureBox1.Image = resourceBitmap;
}
非常感謝您的努力。 如果我可以,什麼是'kvp'? – user2605318
我將它命名爲「kvp」,因爲它代表「鍵 - 值對」。字典項目。 –