在C#中动态添加和加载Resources中的图像

时间:2022-01-03 20:33:13

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.

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

5 个解决方案

#1


14  

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 Action of your image from None to Resource.

您还应该将图像的构建操作从“无”更改为“资源”。

#2


5  

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

您可以打开资源编辑器(解决方案资源管理器,单击Resources.resx)并在其中添加图像。然后,您只需使用Properties.Resources.ImageId将其作为Bitmap访问即可

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

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

#3


1  

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.

我推荐第二个。

#4


0  

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#)

#5


0  

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)位于子文件夹“images”(来自根目录),则确切的语法是:

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.
  • 请注意,“pack:// application:,,,”部分不会更改。
  • Be sure to set the "Build action" to "Resources"
  • 务必将“构建操作”设置为“资源”

For more information: see here.

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

#1


14  

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 Action of your image from None to Resource.

您还应该将图像的构建操作从“无”更改为“资源”。

#2


5  

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

您可以打开资源编辑器(解决方案资源管理器,单击Resources.resx)并在其中添加图像。然后,您只需使用Properties.Resources.ImageId将其作为Bitmap访问即可

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

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

#3


1  

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.

我推荐第二个。

#4


0  

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#)

#5


0  

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)位于子文件夹“images”(来自根目录),则确切的语法是:

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.
  • 请注意,“pack:// application:,,,”部分不会更改。
  • Be sure to set the "Build action" to "Resources"
  • 务必将“构建操作”设置为“资源”

For more information: see here.

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