如何在C#中从项目的Resources文件夹中检索图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/598484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to retrieve Image from Resources folder of the project in C#
提问by Mobin
i Have some images in resources folder in my project but i want to change the picture box from these resource files of the project
我的项目的资源文件夹中有一些图像,但我想从项目的这些资源文件中更改图片框
回答by Daniel LeCheminant
Consider using Properties.Resources.yourImage
考虑使用 Properties.Resources.yourImage
Properties.Resources
contains everything that you've added as a resource (see your project properties, resources tab)
Properties.Resources
包含您作为资源添加的所有内容(请参阅您的项目属性、资源选项卡)
Other than that, if you embed the images as resource in your project, you can get at them by calling GetManifestResourceStream
on the assembly that you've embedded the images in, something like
除此之外,如果您将图像作为资源嵌入到项目中,则可以通过调用GetManifestResourceStream
已嵌入图像的程序集来获取它们,例如
Stream imgStream =
Assembly.GetExecutingAssembly().GetManifestResourceStream(
"YourNamespace.resources.ImageName.bmp");
pictureBox.Image = new Bitmap(imgStream);
Don't forget to mark the image as an embedded resource! (You'll need to set the build action for the image in its properties window)
不要忘记将图像标记为嵌入资源!(您需要在其属性窗口中为图像设置构建操作)
If you're finding that you keep getting back null
from GetManifestResourceStream
, you may be giving the wrong name. (It can be hard to get the names right) Call GetManifestResourceNames
on the assembly; that will give you back all the resource names, and you can find the one in the list that you need.
如果你发现你不断收到回来null
的GetManifestResourceStream
,你可能会给出错误的名称。(可能很难把名字弄对) 召集GetManifestResourceNames
大会;这将返回所有资源名称,您可以在列表中找到所需的名称。
回答by Narayan
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");
}
}
回答by SAQLAIN RAZA
Below is the Code to Fetch Image From Resources Folder. Normally we keep images in Resources Folder. but Sometime we have image name only with us. in that case you can Access images from Resources Folder using Image Name only.
下面是从资源文件夹中获取图像的代码。通常我们将图像保存在资源文件夹中。但有时我们只有图像名称。在这种情况下,您只能使用图像名称访问资源文件夹中的图像。
Below Code will Demonstrate about it.
下面的代码将演示它。
private System.Resources.ResourceManager RM = new System.Resources.ResourceManager("YourAppliacationNameSpace.Properties.Resources", typeof(Resources).Assembly);
PbResultImage.Image = (Image)RM.GetObject(YourPictureName);
- YourAppliacationNameSpacemeans name of your Application.
- YourPictureNamemeans the Picture you Want to access from Resources Folder. but Picture name must be without Extension e.g. (PNG, GIF, JPEG etc)
- YourAppliacationNameSpace表示您的应用程序的名称。
- YourPictureName表示您要从资源文件夹访问的图片。但图片名称必须没有扩展名,例如(PNG、GIF、JPEG 等)
hope i will be beneficial to some one.
希望我会对某人有所帮助。
Thank you.
谢谢你。
回答by Sayed Abolfazl Fatemi
Worked for me:
为我工作:
(Bitmap) Properties.Resources.ResourceManager.GetObject("ImageName");