相关疑难解决方法(0)

WPF - 将图像导入为资源

我最近一直在教自己WinForms,但我现在已经切换到更现代的WPF,这是更好的SOOO!

我现在唯一困惑的是使用图像.现在在WinForms中,您将其作为资源导入,并且在/ bin/Debug文件夹中编译时图像仍然有效.

我无法弄清楚如何在WPF中使用它,当我运行应用程序时图像没有加载,因为图像被保存/Projects/AppName/images/,并且应用程序/Projects/AppName/bin/Debug在我在调试模式下运行时编译成.

我只需要复制我的Images文件夹并将其放在编译应用程序的位置吗?还是有另一种方式.这是我的代码,显示我的图像:

<Image Width="300">
    <Image.Source>
        <BitmapImage DecodePixelWidth="300" UriSource="/images/jamsnaps-dark.png" />
    </Image.Source>
</Image>
Run Code Online (Sandbox Code Playgroud)

wpf image

29
推荐指数
2
解决办法
6万
查看次数

如何获取存储在资源中的图像的Uri

我有两个.png文件添加到我的资源,我需要在进行绑定时访问他们的Uri.

我的xaml代码如下:

<Grid>
  <Image>
    <Image.Source>
       <BitmapImage DecodePixelWidth="10" UriSource="{Binding Path=ImagePath}"/>
    </Image.Source>
  </Image> 
</Grid>
Run Code Online (Sandbox Code Playgroud)

binding使用ImagePath的代码是:

ImagePath = resultInBinary.StartsWith("1") ? Properties.Resources.LedGreen : Properties.Resources.ledRed;
Run Code Online (Sandbox Code Playgroud)

然而

Properties.Resources.LedGreen
Run Code Online (Sandbox Code Playgroud)

返回一个Bitmap而不是String包含该特定图像的Uri.我只是想知道如何提取该值而无需在存储的目录中寻址图像的路径.(老实说,我不确定这是正确的事情,因为我在网上找不到任何类似的情况).

如果有可用的话我甚至可以选择使用的方法,请告诉我.

c# wpf resources xaml binding

10
推荐指数
1
解决办法
2万
查看次数

标签 统计

wpf ×2

binding ×1

c# ×1

image ×1

resources ×1

xaml ×1