WPF - 将图像导入为资源 [英] WPF - Import image as resource
问题描述
我最近一直在教自己WinForms,但我现在已经转向更现代的WPF,这是更好的SOOO!
I have been teaching myself WinForms recently, but I have now switched to the more modern WPF, which is SOOO much better!
目前我唯一感到困惑的是使用图片。现在在WinForms中,您将其作为资源导入,并且在/ bin / Debug文件夹中编译时图像仍然有效。
Only thing i'm confused about at the moment is using images. Now in WinForms you imported it as a resource and the image would still work when compiled in the /bin/Debug folder.
我无法弄清楚如何在WPF中运行,当我运行应用程序时图像无法加载,因为图像保存在 / Projects / AppName / images /
,当我运行它时,应用程序被编译成 / Projects / AppName / 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/Debug
when I run it in Debug mode.
我是否只需要复制我的Images文件夹并将其放在编译应用程序的位置?还是有另一种方式。这是我的代码,显示我的图像:
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:
<Image Width="300">
<Image.Source>
<BitmapImage DecodePixelWidth="300" UriSource="/images/jamsnaps-dark.png" />
</Image.Source>
</Image>
推荐答案
- 创建一个文件夹(例如
images
)在Visual Studio项目中。 - 将图像文件添加到该文件夹。
- 将
Build Action
设置为Resource
(在Properties窗口中,参见这个答案)。 - Create a folder (e.g.
images
) in your Visual Studio Project. - Add the image file(s) to that folder.
- Set their
Build Action
toResource
(in the Properties window, see second image in this answer).
然后写像你已经做过的 UriSource
属性:
Then write the UriSource
property like you already did:
UriSource="/images/jamsnaps-dark.png"
该URI实际上是资源文件包URI ,其中前缀由WPF自动添加。
That URI is effectively a Resource File Pack URI, where the prefix is automatically added by WPF.
这篇关于WPF - 将图像导入为资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!