如何获得存储在资源图像的乌里 [英] How to get a Uri of the image stored in the resources

查看:108
本文介绍了如何获得存储在资源图像的乌里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个巴纽文件添加到我的资源,我需要访问他们的开放的我们做结合的时候。

I have two .png files added to my resources which I need to access their Uri when doing binding.

我的 XAML code是如下:

My xaml code is as followed:

<Grid>
  <Image>
    <Image.Source>
       <BitmapImage DecodePixelWidth="10" UriSource="{Binding Path=ImagePath}"/>
    </Image.Source>
  </Image> 
</Grid>

结合 code。使用 的ImagePath 是:

and the binding code using ImagePath is:

ImagePath = resultInBinary.StartsWith("1") ? Properties.Resources.LedGreen : Properties.Resources.ledRed;

然而

Properties.Resources.LedGreen

返回位图而不是字符串包含特定图像的URI的。
我只是想知道如何提取该值,而不需要解决,它的存储目录图像的路径。 (这老实说我不知道​​是做,因为我无法在网络上找到任何类似的情况正确的事)。

returns a Bitmap instead of String containing the Uri of that particular image. I just want to know how to extract that value without a need to address a path of the image in the directory that it's stored. (Which honestly I am not sure is a right thing to do as I couldn't find any similar situation on the net).

请让我知道如果甚至有一个preferred方法来一个我想如果可以使用。

Please let me know if there is even a preferred method to the one I am trying to use if available.

推荐答案

在你通常不存储图像属性/ Resources.resx 和访问它们的WPF应用程序手段 Properties.Resources 类。

In a WPF application you would usually not store images in Properties/Resources.resx and access them by means of the Properties.Resources class.

相反,你只需将图像文件添加到您的Visual Studio项目为常规文件,或许是一个名为图像或类似的文件夹中。然后你会设置它们的生成操作资源,它在属性窗口中完成的。你到达那里例如通过右键单击图像文件,然后选择属性菜单项。需要注意的是默认值生成操作资源的反正图像文件。

Instead you just add the image files to your Visual Studio project as regular files, perhaps in a folder named "Images" or the like. Then you would set their Build Action to Resource, which is done in the Properties window. You get there e.g. by right-clicking the image file and select the Properties menu item. Note that the default value of the Build Action should be Resource for image files anyways.

为了从$ C $访问这些图片资源c您会再使用包URI 。有了上面的文件夹名称为图像,并命名为LedGreen.png图像文件,创建这样的URI是这样的:

In order to access these image resources from code you would then use a Pack URI. With the above folder name "Images" and an image file named "LedGreen.png", creating such an URI would look like this:

var uri = new Uri("pack://application:,,,/Images/LedGreen.png");

所以,你也许可以宣布你的财产是开放的类型的:

So you could perhaps declare your property to be of type Uri:

public Uri ImageUri { get; set; } // omitted INotifyPropertyChanged implementation

和设置是这样的:

ImageUri = resultInBinary.StartsWith("1")
         ? new Uri("pack://application:,,,/Images/LedGreen.png")
         : new Uri("pack://application:,,,/Images/LedRed.png");

最后你的XAML应该像下图所示,它依赖于内置的类型转换,从URI来ImageSource的:

Finally your XAML should look like shown below, which relies on built-in type conversion from Uri to ImageSource:

<Grid>
    <Image Width="10" Source="{Binding Path=ImageUri}" />
</Grid>

这篇关于如何获得存储在资源图像的乌里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆