gpt4 book ai didi

c# - 在 Visual Studio 中使用图片框

转载 作者:行者123 更新时间:2023-11-30 22:15:49 24 4
gpt4 key购买 nike

我目前正在使用以下代码将图像加载到图片框上。

pictureBox1.Image = Properties.Resources.Desert;

我会用“变量”替换“沙漠”,因为代码将按如下方式工作。

String Image_Name;
Imgage_Name = "Desert";
pictureBox1.Image = Properties.Resources.Image_Name;

我有很多需要加载的 Imagines,我想使用一个变量作为图像名称,而不必为每个图像单独写一行。这可能吗 ?

最佳答案

您可以遍历资源......像这样:

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

关于c# - 在 Visual Studio 中使用图片框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17779263/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com