简体   繁体   English

wpf图像资源和visual studio 2010资源编辑器

[英]wpf Image resources and visual studio 2010 resource editor

My motivation for this question is really just to specify an image to be used in a user control via a dependency property for ImageSource. 我对这个问题的动机实际上只是通过ImageSource的依赖属性指定要在用户控件中使用的图像。 I'm hitting some pain points involving the management, access, and unit testing for this. 我正在遇到一些涉及管理,访问和单元测试的痛点。

  • Is the resource editor a good tool to use to maintain images for the application? 资源编辑器是否是用于维护应用程序映像的好工具?
  • What is the best way to translate the Bitmap from the editor to an ImageSource? 将Bitmap从编辑器转换为ImageSource的最佳方法是什么?
  • How can I grab the resource Filename from the editor? 如何从编辑器中获取资源Filename?

No, the resource editor is not a good tool for this. 不,资源编辑器不是一个好的工具。

In a WPF application the best way is to put all of your images in an "Images" directory and mark each one as a "Resource". 在WPF应用程序中,最好的方法是将所有图像放在“图像”目录中,并将每个图像标记为“资源”。 Then you can reference them directly in Image controls and elsewhere. 然后,您可以直接在图像控件和其他地方引用它们。

Here are the precise steps: 以下是精确的步骤:

  • Crop and otherwise adjust your images using your favorite bitmap editing program (Paint.NET, Photoshop, etc) 使用您喜欢的位图编辑程序(Paint.NET,Photoshop等)裁剪和调整图像
  • Save them as .png files (or .jpg or .gif if you prefer) 将它们保存为.png文件(如果您愿意,可以将.jpg或.gif文件保存)
  • Create an "Images" folder inside your Visual Studio solution (or multiple folders, however you want to organize it) 在Visual Studio解决方案中创建“Images”文件夹(或多个文件夹,但是要组织它)
  • Drag the images from your hard disk into your "Images" folder (or right-click the project, select New -> Existing Item and select the images) 将图像从硬盘拖到“Images”文件夹中(或右键单击项目,选择New - > Existing Item并选择图像)

Now you can reference your images easily in XAML: 现在,您可以在XAML中轻松引用图像:

<Image Source="Images/MyImage.png" />

Or in code: 或者在代码中:

var source = (BitmapSource)Application.LoadComponent(
               new Uri("Images/MyImage.png", UriKind.Relative));

You can also reference images in external assemblies: 您还可以在外部装配中引用图像:

<Image Source="ReferencedAssembly;v1.0.0.1;component/Images/MyImage.png" />

Which in code would be: 在代码中将是:

var source = (BitmapSource)Application.LoadComponent(
               new Uri("ReferencedAssembly;v1.0.0.1;component/Images/MyImage.png",
                 UriKind.Relative));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM