从 C# 项目的资源区域加载图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1192054/
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-06 10:27:24  来源:igfitidea点击:

Load image from resources area of project in C#

c#imagewinformsload

提问by Pavel Bastov

I have an image in my project stored at Resources/myimage.jpg. How can I dynamically load this image into Bitmap object?

我的项目中有一个图像存储在 Resources/myimage.jpg 中。如何将此图像动态加载到 Bitmap 对象中?

采纳答案by Matt Hamilton

Are you using Windows Forms? If you've added the image using the Properties/Resources UI, you get access to the image from generated code, so you can simply do this:

您在使用 Windows 窗体吗?如果您使用属性/资源 UI 添加了图像,则可以从生成的代码访问图像,因此您可以简单地执行以下操作:

var bmp = new Bitmap(WindowsFormsApplication1.Properties.Resources.myimage);

回答by Charlie Salts

You can get a reference to the image the following way:

您可以通过以下方式获取对图像的引用:

Image myImage = Resources.myImage;

If you want to make a copyof the image, you'll need to do the following:

如果要复制图像,则需要执行以下操作:

Bitmap bmp = new Bitmap(Resources.myImage);

Don't forget to dispose of bmpwhen you're done with it. If you don't know the name of the resource image at compile-time, you can use a resource manager:

完成后不要忘记处理bmp。如果在编译时不知道资源映像的名称,可以使用资源管理器:

ResourceManager rm = Resources.ResourceManager;
Bitmap myImage = (Bitmap)rm.GetObject("myImage");

The benefit of the ResourceManager is that you can use it where Resources.myImage would normally be out of scope, or where you want to dynamically access resources. Additionally, this works for sounds, config files, etc.

ResourceManager 的好处是你可以在 Resources.myImage 通常超出范围的地方使用它,或者你想动态访问资源的地方。此外,这适用于声音、配置文件等。

回答by Josip Medved

You need to load it from resource stream.

您需要从资源流加载它。

Bitmap bmp = new Bitmap(
  System.Reflection.Assembly.GetEntryAssembly().
    GetManifestResourceStream("MyProject.Resources.myimage.png"));

If you want to know all resource names in your assembly, go with:

如果您想知道程序集中的所有资源名称,请使用:

string[] all = System.Reflection.Assembly.GetEntryAssembly().
  GetManifestResourceNames();

foreach (string one in all) {
    MessageBox.Show(one);
}

回答by Hallgrim

The best thing is to add them as Image Resources in the Resources settings in the Project. Then you can get the image directly by doing Resources.myimage. This will get the image via a generated C# property.

最好的办法是在项目的资源设置中将它们添加为图像资源。然后你就可以直接通过Resources.myimage获取图片了。这将通过生成的 C# 属性获取图像。

If you just set the image as Embedded Resource you can get it with:

如果您只是将图像设置为嵌入式资源,则可以通过以下方式获取:

string name = "Resources.myimage.jpg"
string namespaceName = "MyCompany.MyNamespace";
string resource = namespaceName + "." + name;
Type type = typeof(MyCompany.MyNamespace.MyTypeFromSameAssemblyAsResource);
Bitmap image = new Bitmap(type.Assembly.GetManifestResourceStream(resource));

Where MyTypeFromSameAssemblyAsResource is any type that you have in your assembly.

其中 MyTypeFromSameAssemblyAsResource 是程序集中的任何类型。

回答by Violator

With and ImageBox named "ImagePreview FormStrings.MyImageNames contains a regular get/set string cast method, which are linked to a scrollbox type list. The images have the same names as the linked names on the list, except for the .bmp endings. All bitmaps are dragged into the resources.resx

和 ImageBox 命名为“ImagePreview FormStrings.MyImageNames 包含一个常规的 get/set 字符串转换方法,它链接到一个滚动框类型列表。图像与列表中的链接名称具有相同的名称,除了 .bmp 结尾。所有位图被拖入resources.resx

Object rm = Properties.Resources.ResourceManager.GetObject(FormStrings.MyImageNames);
Bitmap myImage = (Bitmap)rm;
ImagePreview.Image = myImage;

回答by Loren Pechtel

Strangely enough, from poking in the designer I find what seems to be a much simpler approach:

奇怪的是,从设计师的角度来看,我发现似乎是一种更简单的方法:

The image seems to be available from .Properties.Resources.

该图像似乎可从 .Properties.Resources 获得。

I'm simply using an image as all I'm interested in is pasting it into a control with an image on it.

我只是使用图像,因为我感兴趣的只是将其粘贴到带有图像的控件中。

(Net 4.0, VS2010.)

(网络 4.0,VS2010。)

回答by JDS

Way easier than most all of the proposed answers

比大多数建议的答案都容易

tslMode.Image = global::ProjectName.Properties.Resources.ImageName;

回答by DSmith

In my case -- I was using Iconsin my resource, but I needed to add them dynamically as Imagesto some ToolStripMenuItem(s). So in the method that I created (which is where the code snippet below comes from), I had to convert the icon resources to bitmaps before I could return them for addition to my MenuItem.

就我而言 - 我在我的资源中使用图标,但我需要将它们作为图像动态添加到某些ToolStripMenuItem(s)中。所以在我创建的方法中(这是下面的代码片段的来源),我必须将图标资源转换为位图,然后才能返回它们以添加到我的MenuItem.

string imageName = myImageNameStr;
imageName = imageName.Replace(" ", "_");
Icon myIcon = (Icon)Resources.ResourceManager.GetObject(imageName);
return myIcon.ToBitmap();

Something else to be aware of, if your image/icon has spaces (" ") in its name when you add them to your resource, VS will automatically replace those spaces with "_"(s). Because, spaces are not a valid character when naming your resource. Which is why I'm using the Replace()method in my referenced code. You can likely just ignore that line.

还有一点需要注意,如果您的图像/图标在您将它们添加到您的资源时其名称中有空格(“”),VS 将自动用“_”(s)替换这些空格。因为,在命名资源时,空格不是有效字符。这就是我Replace()在引用的代码中使用该方法的原因。您可能可以忽略该行。

回答by Gary Kindel

Code I use in several of my projects... It assumes that you store images in resource only as bitmaps not icons

我在我的几个项目中使用的代码......它假设您将图像仅作为位图而不是图标存储在资源中

    public static Bitmap GetImageByName(string imageName)
    {
        System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
        string resourceName = asm.GetName().Name + ".Properties.Resources";
        var rm = new System.Resources.ResourceManager(resourceName, asm);
        return (Bitmap)rm.GetObject(imageName);

    }

回答by Davide Icardi

I suggest:

我建议:

System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file = 
    thisExe.GetManifestResourceStream("AssemblyName.ImageFile.jpg");
Image yourImage = Image.FromStream(file);

From msdn: http://msdn.microsoft.com/en-us/library/aa287676(v=vs.71).aspx

来自 msdn:http: //msdn.microsoft.com/en-us/library/aa287676(v=vs.71) .aspx

Using Image.FromStream is better because you don't need to know the format of the image (bmp, png, ...).

使用 Image.FromStream 更好,因为您不需要知道图像的格式(bmp、png、...)。