13

我在 Resources.resx 中包含了一个图标文件,我想在堆栈面板内的 TreeViewItem 上显示该文件。

1) .ico 文件可以用于此目的吗?还是必须是 .bmp 或 jpg?

2)您在 XAML 中将源设置为什么?以下代码对我不起作用

<StackPanel Orientation="Horizontal">
    <Image Margin="2" Source="/Resources/Folder_Back.ico" />
    <TextBlock Margin="2" Text="{Binding ProgramName}"
     Foreground="White" FontWeight="Bold"/>
</StackPanel>
4

5 回答 5

11

这是访问资源文件中图像的技巧:

从 XAML 标记中的资源文件访问图像

首先,您需要添加对项目属性的引用,如下所示:

xmlns:properties="clr-namespace:MyProject.Properties"

然后像这样通过 XAML 访问它:

<image source="{Binding Source={x:Static properties:Resources.ImageName}}" />

您可以使用 PNG/JPG/BMP 以及 ICO 文件,但每个人都推荐 PNG。

于 2012-09-11T06:38:44.057 回答
8

要使 Qorbani 的解决方案起作用,请在 Image Source.Binding 中添加一个转换器!

XAML - 命名空间

 xmlns:properties="clr-namespace:YourNameSpace.Properties"
 xmlns:converter="clr-namespace:YourNameSpace.Converter"

Xaml - 资源(用户控件或窗口)

 <UserControl.Resources>
        <ResourceDictionary>
              <converter:BitmapToImageSourceConverter x:Key="BitmapToImageSourceConverter" />
        </ResourceDictionary>
 </UserControl.Resources>

Xaml 代码

<StackPanel Orientation="Horizontal">
                    <Image Width="32" Height="32" Source="{Binding Source={x:Static properties:Resources.Import}, Converter={StaticResource BitmapToImageSourceConverter}}" Stretch="Fill" />
                    <TextBlock Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center">Import</TextBlock>
</StackPanel>

BitmapToImageSourceConverter.cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace YourNameSpace
{
    public class BitmapToImageSourceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var bitmap = value as System.Drawing.Bitmap;
            if (bitmap == null)
                throw new ArgumentNullException("bitmap");

            var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

            var bitmapData = bitmap.LockBits(
                rect,
                ImageLockMode.ReadWrite,
                System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            try
            {
                var size = (rect.Width * rect.Height) * 4;

                return BitmapSource.Create(
                    bitmap.Width,
                    bitmap.Height,
                    bitmap.HorizontalResolution,
                    bitmap.VerticalResolution,
                    PixelFormats.Bgra32,
                    null,
                    bitmapData.Scan0,
                    size,
                    bitmapData.Stride);
            }
            finally
            {
                bitmap.UnlockBits(bitmapData);
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
于 2016-06-16T08:13:21.547 回答
7

你不能那样做。仅在winforms中有效

看到这篇文章了解更多信息

将图像添加到资源的不同方式

使用这篇文章中显示的方法

WPF 图片资源

反而

引用:

如果您将在多个地方使用图像,那么值得将图像数据仅加载一次到内存中,然后在所有Image元素之间共享它。

为此,请BitmapSource 在某处创建一个资源:

<BitmapImage x:Key="MyImageSource" UriSource="../Media/Image.png" />

然后,在您的代码中,使用类似:

<Image Source="{StaticResource MyImageSource}" />

就我而言,我发现我必须将Image.png文件设置为具有构建操作,Resource而不仅仅是Content. 这会导致图像在您编译的程序集中携带。

于 2012-09-11T06:29:38.410 回答
3

接受的答案说这是不可能的,工作解决方案将 GDI+Bitmap类型转换为 WPF 图像。但是这些转换是完全没有必要的。解决方案其实很简单:

  1. 当您将图像或图标文件添加到资源文件时,设计器默认为它们选择 GDI+ 类型:

在此处输入图像描述

  1. 只需在 XML 编辑器中打开 .resx 文件(在解决方案资源管理器中右键单击,打开方式...),然后更改BitmapIcon键入MemoryStream
<!--<value>..\Resources\Undo.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>-->
<value>..\Resources\Undo.png;System.IO.MemoryStream</value>

...

<!--<value>..\Resources\Error.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>-->
<value>..\Resources\Error.ico;System.IO.MemoryStream</value>

  1. 保存 .resx 文件。如果您现在打开设计器,您可以在“其他”菜单下找到您的资源。

在此处输入图像描述

不要为“修复”而烦恼Resources.cs。当您保存 .resx 文件时,它将使用正确的类型自动重新生成。生成的返回类型实际上是UnmanagedMemoryStream,但不要对此感到困惑。

  1. 用法:
public static class WpfImages
{
    public static ImageSource Error { get; } = BitmapFrame.Create(Resources.Error);
    // [...]
}
<Image Source="{x:Static local:WpfImages.Error}"/>
于 2019-07-18T17:24:51.980 回答
1

首先:添加资源 rsx 然后:将图像作为图像添加到资源文件中,并将图像构建操作设置为 Resource。现在您可以像这样访问图像:

<Image Source="pack://application:,,,/Resources/image.png"/>
于 2018-11-25T17:33:59.383 回答