如何使用Resources.resx链接图片

14

我在Resources.resx中包含了一个图标文件,我希望将其显示在StackPanel内的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>
5个回答

11

这里有一个访问资源文件中图像的技巧:

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

首先,您需要像这样在项目属性中添加引用:

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

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

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

你可以使用PNG/JPG/BMP格式以及ICO图标文件,但是大家都推荐使用PNG。


10
无效,没有任何显示。我尝试将其用作图标和图片。 - The Muffin Man

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();
        }
    }
}

它可以工作。Qorbani的答案对于图像无效。但是在执行后我遇到了一些错误。“staticExtension value cannot be resolved to an enumeration, static field, or static property”。解决方案请参考https://dev59.com/g2kw5IYBdhLWcg3wmryH。 - jijijijiji

7

你不能这样做。这只适用于WinForms。

查看此帖子以获取更多信息:

不同的方法如何将图像添加到资源

请使用此帖子中显示的方法:

WPF图像资源

代替。

引用:

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

为此,请在某个地方创建一个BitmapSource作为资源:

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

然后,在您的代码中,使用类似以下的内容:

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

在我的情况下,我发现我必须将Image.png文件的构建操作设置为Resource而不仅仅是Content。这会导致图像被包含在编译后的程序集中。

我可以使用.ico文件作为图像源吗? - TtT23
是的,请使用.ico而不是png。您甚至可以在VS内部进行编辑。 - Nahum

4
接受的答案表示不可能,并且工作解决方案将GDI+ Bitmap类型转换为WPF图像。但这些转换是完全不必要的。解决方案实际上非常简单:
  1. 将图像或图标文件添加到资源文件时,默认情况下设计师会选择GDI+类型:

进入图像描述

  1. 只需在XML编辑器中打开.resx文件(在“解决方案资源管理器”中右键单击,选择使用...),并将 Bitmap Icon 类型更改为 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文件。如果现在打开设计器,您可以在“其他”菜单下找到您的资源。

enter image description here

不用费力去“修复”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}"/>

2

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

<Image Source="pack://application:,,,/Resources/image.png"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接