您可以遍历资源.. 像这样:
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;
}