如何在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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 09:45:46  来源:igfitidea点击:

How to retrieve Image from Resources folder of the project in C#

c#winformsresources

提问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.Resourcescontains 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 GetManifestResourceStreamon 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 nullfrom GetManifestResourceStream, you may be giving the wrong name. (It can be hard to get the names right) Call GetManifestResourceNameson the assembly; that will give you back all the resource names, and you can find the one in the list that you need.

如果你发现你不断收到回来nullGetManifestResourceStream,你可能会给出错误的名称。(可能很难把名字弄对) 召集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");

回答by Hassan Rahman

Right click on the project. Go to Resources tab. Select the existing option and add the image.

右键单击项目。转到资源选项卡。选择现有选项并添加图像。

enter image description here

在此处输入图片说明

Now access in the code by

现在通过代码访问

Image = Properties.Resources.ImageName