![](/img/trans.png)
[英]How to retrieve .dotx and .csv files from Resources Folder in C#?
[英]How to retrieve Image from Resources folder of the project in C#
我的项目的资源文件夹中有一些图像,但我想从项目的这些资源文件中更改图片框
考虑使用Properties.Resources.yourImage
Properties.Resources
包含您添加为资源的所有内容(请参见项目属性,资源选项卡)
除此之外,如果您将图像作为资源嵌入到项目中,则可以通过在嵌入图像的程序集上调用GetManifestResourceStream
来获取它们,例如
Stream imgStream =
Assembly.GetExecutingAssembly().GetManifestResourceStream(
"YourNamespace.resources.ImageName.bmp");
pictureBox.Image = new Bitmap(imgStream);
不要忘记将图像标记为嵌入式资源! (您需要在图像的属性窗口中设置图像的构建动作)
如果发现从GetManifestResourceStream
继续返回null
,则可能输入了错误的名称。 (很难正确地命名)在程序集上调用GetManifestResourceNames
。 这将为您提供所有资源名称,您可以在列表中找到所需的名称。
string img = null;
private void btnShow_Click(object sender, EventArgs e)
{
string imgName;
img = textBox1.Text;
imgName = "images/" + img + ".jpg";
if (imgName == null)
{
MessageBox.Show("no photo");
}
else if (imgName != null)
{
this.picBox1.Image = Image.FromFile("images/" + img + ".jpg");
}
}
下面是从资源文件夹中获取图像的代码。 通常我们将图像保存在资源文件夹中。 但有时我们只有图像名称。 在这种情况下,您只能使用“映像名称”从“资源”文件夹访问映像。
下面的代码将对此进行演示。
private System.Resources.ResourceManager RM = new System.Resources.ResourceManager("YourAppliacationNameSpace.Properties.Resources", typeof(Resources).Assembly);
PbResultImage.Image = (Image)RM.GetObject(YourPictureName);
希望我对某人有益。
谢谢。
为我工作:
(Bitmap) Properties.Resources.ResourceManager.GetObject("ImageName");
如果您需要通过字符串或连接引用调用您的控件,这也适用于您的控件。
只需先调用控件类型(在本例中为图片框)。 假设 i=某个数字和 d=某个字符串:
var i = <some number>;
var d = <some string>;
((PictureBox)this.Controls["pictureBox" + i.ToString()]).Image = (Image)Properties.Resources.ResourceManager.GetObject("dice" + d);
现在有可能
pictureBox.Image = Properties.Resources.yourImagename;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.