开发者

WPF Get Image From RESX File

开发者 https://www.devze.com 2023-02-19 18:50 出处:网络
In WPF XAML I\'ve got an Image tag and I\'m trying to set the source to an image in my resx file. Fou.png is set to Build Action of Embedded Resource.

In WPF XAML I've got an Image tag and I'm trying to set the source to an image in my resx file. Fou.png is set to Build Action of Embedded Resource. For the life of me I can't get the source correct, it keeps say that Fou.png开发者_运维百科 isn't part of the project even though I can see it in the Resouces foulder/Resx file. I've tried these below to no avail

Ideas


Build Action needs to be "Resource" not "Embedded Resource"


1st of all "Embedded Resource" is not advised in WPF (cant remember why, but if you google it you will find the explanation)

The way I load my resources:

a) In Visual studio add a "Resources" folder to your project and add your images inside it.

b) Then in XAML you can access them like that:

<Image Width="18" Source="/MyApplication;component/Resources/Foo.png" />

In this example "MyApplication" is the name of the assembly (see 1st line of your XAMl x:Class= to get the name of your assembly) "Resources" the name of the folder containing the resources and "Foo.png" the name of the image.

Good luck

JM


Make sure that you've set width and height properties of your Image control. At least in simplest test project Image doesn't show unless Width and Height are set.


<Image x:Name="myImage" Source="..\Resources\icon.jpg"></Image>

Properties: Set the Build Action to "Resource" instead of "Embedded Resource"

In the sample test project that I created, I had an image file named icon.jpg added to a Resouce file named MyResource.resx. The Width and Height to the Image in XAML are optional

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号