在 C# 中从资源动态添加和加载图像

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

Dynamically adding and loading image from Resources in C#

c#wpfimageresources

提问by logeeks

I have some images added to my solution, right now it is under the folder images\flowers\rose.png inside the solution explorer. I want a way to dynamically load this image to my image control.

我在我的解决方案中添加了一些图像,现在它位于解决方案资源管理器内的文件夹 images\flowers\rose.png 下。我想要一种将这个图像动态加载到我的图像控件的方法。

My current approach is making the type 'content' and use 'copy always' properties. Then i would give relative path to the image like below.

我目前的方法是制作“内容”类型并使用“始终复制”属性。然后我会给出图像的相对路径,如下所示。

Image2.Source = new BitmapImage(new Uri("/images/flowers/Customswipe_b.png", UriKind.Relative));

Is there any way to make it load from the resource without copying it to the target system.

有没有办法让它从资源加载而不将它复制到目标系统。

回答by MrDosu

You can open the Resource Editor (Solution Explorer, click on Resources.resx) and add the image there. Then you can simply access it as Bitmapwith Properties.Resources.ImageId

您可以打开资源编辑器(解决方案资源管理器,单击 Resources.resx)并在那里添加图像。然后,你可以简单地访问它BitmapProperties.Resources.ImageId

http://msdn.microsoft.com/en-us/library/3bka19x4(v=vs.100).aspx

http://msdn.microsoft.com/en-us/library/3bka19x4(v=vs.100).aspx

回答by HichemSeeSharp

You use this :

你用这个:

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

Or

或者

Image2.Source = new Bitmap(WindowsFormsApplication1.Properties.Resources.myimage);

I do recommend the second one.

我确实推荐第二个。

回答by Eric

The following works just fine for me:

以下对我来说很好用:

image.Source = new BitmapImage(new Uri("pack://application:,,,/YourAssemblyName;component/Resources/someimage.png", UriKind.Absolute));

Also you should change the Build Actionof your image from Noneto Resource.

此外,您应该Build Action将图像的更改NoneResource

回答by DeshDeep Singh

The way you are adding image and then changing its "Build action" to "Resources" will also do the job, But as you have asked for adding and loading from Resources wold be a different approach to achieve the same task. I would liketo provide you a link to read certain msdn articles.

您添加图像然后将其“构建操作”更改为“资源”的方式也可以完成这项工作,但是正如您所要求的那样,从资源添加和加载将是实现相同任务的不同方法。我想为您提供一个链接来阅读某些 msdn 文章。

Adding and Editing Resources (Visual C#)

添加和编辑资源 (Visual C#)

回答by Malick

I had some problems to find the exact syntax for the URI, so see below more details :

我在查找 URI 的确切语法时遇到了一些问题,因此请参阅下面的更多详细信息:

If your image (myImage.png) is located in a subfolder "images" (from the root directory) , the exact syntax is :

如果您的图像 ( myImage.png) 位于子文件夹“图像”(来自根目录)中,则确切的语法是:

image.Source = new BitmapImage(new Uri(@"pack://application:,,,/images/myImage.png", UriKind.Absolute));

If your image is in the subfolder images/icon/(from the root directory) , the syntax is :

如果您的图像位于子文件夹中images/icon/(来自根目录),则语法为:

image.Source = new BitmapImage(new Uri(@"pack://application:,,,/images/icon/myImage.png", UriKind.Absolute));
  • Note that the part "pack://application:,,,does not change.
  • Be sure to set the "Build action" to "Resources"
  • 请注意,该部分"pack://application:,,,不会改变。
  • 请务必将“构建操作”设置为“资源”

For more information: see here.

有关更多信息:请参阅此处