【问题标题】:How to use Resources.resx to link images如何使用 Resources.resx 链接图像
【发布时间】:2012-09-04 00:52:41
【问题描述】:

我在 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>

【问题讨论】:

    标签: c# .net wpf image resources


    【解决方案1】:

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

    Accessing image from Resource File in XAML markup

    首先你需要像这样添加对项目属性的引用:

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

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

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

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

    【讨论】:

    • 这不起作用。什么都没有出现。我尝试将它用于图标和图像。
    【解决方案2】:

    为了使 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 值无法解析为枚举、静态字段或静态属性”。解决方案是stackoverflow.com/questions/9773610/…
    【解决方案3】:

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

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

    Different way how add image to resources

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

    WPF image resources

    改为

    引用:

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

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

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

    然后,在您的代码中,使用如下内容:

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

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

    【讨论】:

    • 我可以使用 .ico 文件作为图像源吗?
    • 是的。只需使用 .ico 而不是 png。你甚至可以在 VS 中编辑它
    【解决方案4】:

    接受的答案说这是不可能的,工作解决方案将 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 文件。如果您现在打开设计器,您可以在 Other 菜单下找到您的资源。

    不要为“修复”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}"/>
    

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-15
        • 2020-12-24
        • 1970-01-01
        相关资源
        最近更新 更多