wpf 图像资源和 Visual Studio 2010 资源编辑器

发布于 2024-09-05 12:51:53 字数 180 浏览 6 评论 0原文

我提出这个问题的动机实际上只是通过 ImageSource 的依赖属性指定要在用户控件中使用的图像。我遇到了一些涉及管理、访问和单元测试的痛点。

  • 资源编辑器是用于维护应用程序图像的好工具吗?
  • 将位图从编辑器转换为图像源的最佳方法是什么?
  • 如何从编辑器中获取资源文件名?

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. 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?
  • How can I grab the resource Filename from the editor?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清音悠歌 2024-09-12 12:51:53

不,资源编辑器不是一个很好的工具。

在 WPF 应用程序中,最好的方法是将所有图像放入“图像”目录中,并将每个图像标记为“资源”。然后您可以直接在图像控件和其他地方引用它们。

以下是精确的步骤:

  • 使用您最喜欢的位图编辑程序(Paint.NET、Photoshop 等)裁剪和调整图像
  • 将它们保存为 .png 文件(如果您愿意,也可以保存为 .jpg 或 .gif)
  • 创建“图像”文件夹在 Visual Studio 解决方案(或多个文件夹,但您想要组织它)
  • 中将图像从硬盘拖到“图像”文件夹中(或右键单击项目,选择“新建”->“现有项目”并选择图像)

现在,您可以在 XAML 中轻松引用图像:

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

或者在代码中:

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

您还可以在外部程序集中引用图像:

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

在代码中将是:

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

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". 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)
  • Save them as .png files (or .jpg or .gif if you prefer)
  • Create an "Images" folder inside your Visual Studio solution (or multiple folders, however you want to organize it)
  • Drag the images from your hard disk into your "Images" folder (or right-click the project, select New -> Existing Item and select the images)

Now you can reference your images easily in 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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文