WPF - 将图像作为资源导入

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

WPF - Import image as resource

wpfimage

提问by Martyn Ball

In WinForms it is possible to import an image as a resource, and the image would still work when compiled in the /bin/Debug folder.

在 WinForms 中,可以将图像作为资源导入,并且该图像在 /bin/Debug 文件夹中编译时仍然可以工作。

I can't figure out how to get this working in WPF, when I run the application the image doesn't load, because the image is saved in /Projects/AppName/images/, and the application is compiled into /Projects/AppName/bin/Debugwhen I run it in Debug mode.

我无法弄清楚如何得到这个工作在WPF中,当我运行图像不加载应用程序,这是因为图像保存在/Projects/AppName/images/,并且应用程序被编译成/Projects/AppName/bin/Debug,当我在调试模式下运行它。

Do I simply need to make a copy of my Images folder and put it where the application is compiled? Or is there another way. Here is my code which displays my image:

我是否只需要复制我的 Images 文件夹并将其放在编译应用程序的位置?或者有没有别的办法。这是显示我的图像的代码:

<Image Width="300">
    <Image.Source>
        <BitmapImage DecodePixelWidth="300" UriSource="/images/jamsnaps-dark.png" />
    </Image.Source>
</Image>

回答by Clemens

  • Create a folder (e.g. images) in your Visual Studio Project.
  • Add the image file(s) to that folder.
  • Set their Build Actionto Resource(in the Properties window, see second image in this answer).
  • images在您的 Visual Studio 项目中创建一个文件夹(例如)。
  • 将图像文件添加到该文件夹​​。
  • 将它们设置Build ActionResource(在“属性”窗口中,请参阅此答案中的第二张图片)。

Then write the UriSourceproperty like you already did:

然后UriSource像你已经做的那样编写属性:

UriSource="/images/jamsnaps-dark.png"

That URI is effectively a Resource File Pack URI, where the prefix is automatically added by WPF.

该 URI 实际上是一个Resource File Pack URI,其中前缀由 WPF 自动添加。

回答by codeafin

  1. Add the image to your project directory
  2. In the solutions explorer, right click the image and select Include in Project
  1. 将图像添加到您的项目目录
  2. 在解决方案资源管理器中,右键单击图像并选择 Include in Project

Build Action should be set to Resourceby default.

Resource默认情况下,构建操作应设置为。

You can then start to use the image path relative to the root e.g. images/text.jpgif you put it in a folder called images.

然后,您可以开始使用相对于根目录的图像路径,例如,images/text.jpg如果您将其放在名为images.

回答by eran otzap

Two options :

两种选择:

1) Go out from bin/Debug and in to your application folder by ../../ and then to your image.

1) 从 bin/Debug 退出并通过 ../../ 进入您的应用程序文件夹,然后进入您的图像。

  <Image>
      <Image.Source>
           <BitmapImage  UriSource="../../images/jamsnaps-dark.png" />
      </Image.Source>
   </Image>

2) Compile your images as Content from properties in the context menu on the image file , and then when compiled they would be placed in been debug. I don't remember if you also need to tell them to copy local also in properties ( i'm not near a computer at the moment so i can't check .

2)从图像文件的上下文菜单中的属性中将图像编译为内容,然后在编译时将它们放置在调试中。我不记得你是否还需要告诉他们在属性中也复制本地(我现在不在电脑附近,所以我无法检查 .