如何使用Properties.Resources中的图像从WPF中的代码隐藏动态更改图像源?

dta*_*lor 8 wpf imagesource

我有一个WPF应用程序,需要向用户提供有关内部状态的反馈.设计是有三个图像,称为红色,黄色和绿色.其中一个图像将根据状态一次显示.以下是要点:

  • 这三个图像位于代码隐藏中的Properties.Resources中
  • 一次只能显示一个图像.
  • 状态更改来自代码隐藏中的进程,而不是来自用户.
  • 我想绑定一个图像控件,以便我可以从代码隐藏更改图像.

我假设我需要一个图像转换器来将JPG图像更改为图像源,例如:


[ValueConversion(typeof(System.Drawing.Bitmap), typeof(ImageSource))]
public class BitmapToImageSourceConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var bmp = value as System.Drawing.Bitmap;
        if (bmp == null)
            return null;
        return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    bmp.GetHbitmap(),
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

我更喜欢在初始化期间转换图像一次并保留图像源列表.我也假设我需要一个依赖属性来绑定控件,但我不知道如何使用这个图像源列表进行设置:


    // Dependancy Property for the North Image
    public static readonly DependencyProperty NorthImagePathProperty
        = DependencyProperty.Register(
            "NorthImagePath",
            typeof(ImageSource),
            typeof(MainWindow),
            new PropertyMetadata("**Don't know what goes here!!!**"));

    // Property wrapper for the dependancy property
    public ImageSource NorthImagePath
    {
        get { return (ImageSource)GetValue(NorthImagePathProperty); }
        set { SetValue(NorthImagePathProperty, value); }
    }
Run Code Online (Sandbox Code Playgroud)

Cle*_*ens 32

虽然WPF项目中的图像资源会生成System.Drawing.Bitmap属性Resources.Designer.cs,但您可以直接BitmapImage从该资源创建.您只需要将图像文件的构建操作设置为Resource(而不是默认值None).

如果Visual Studio项目Red.jpgResources文件夹中有文件,则创建BitmapImage如下所示.它使用WPF Pack Uri.

var uri = new Uri("pack://application:,,,/Resources/Red.jpg");
var bitmap = new BitmapImage(uri);
Run Code Online (Sandbox Code Playgroud)

如果你Image在XAML中的某个地方声明了一个控件,就像这样:

<Image x:Name="image"/>
Run Code Online (Sandbox Code Playgroud)

你可以简单地Source在后面的代码中将图像的属性设置为你的BitmapImage:

image.Source = bitmap;
Run Code Online (Sandbox Code Playgroud)

如果您希望Source通过绑定设置属性,则可以创建一个string返回图像URI 的属性.该字符串将自动BitmapImageTypeConverterWPF中的内置转换为a .

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        ImageUri = "pack://application:,,,/Resources/Red.jpg";
    }

    public static readonly DependencyProperty ImageUriProperty =
        DependencyProperty.Register("ImageUri", typeof(string), typeof(MainWindow));

    public string ImageUri
    {
        get { return (string)GetValue(ImageUriProperty); }
        set { SetValue(ImageUriProperty, value); }
    }
}
Run Code Online (Sandbox Code Playgroud)

在XAML中,您将绑定到该属性,如下所示:

<Image Source="{Binding ImageUri}"/>
Run Code Online (Sandbox Code Playgroud)

当然,您也可以将属性声明为类型 ImageSource

public static readonly DependencyProperty ImageProperty =
    DependencyProperty.Register("Image", typeof(ImageSource), typeof(MainWindow));

public ImageSource Image
{
    get { return (ImageSource)GetValue(ImageProperty); }
    set { SetValue(ImageProperty, value); }
}
Run Code Online (Sandbox Code Playgroud)

并以相同的方式绑定:

<Image Source="{Binding Image}"/>
Run Code Online (Sandbox Code Playgroud)

现在,您可以预先加载图像并根据需要将它们放入属性中:

private ImageSource imageRed =
    new BitmapImage(new Uri("pack://application:,,,/Resources/Red.jpg"));
private ImageSource imageBlue =
    new BitmapImage(new Uri("pack://application:,,,/Resources/Blue.jpg"));
...
Image = imageBlue;
Run Code Online (Sandbox Code Playgroud)

更新:毕竟,您的图像不需要是Visual Studio项目中的资源.您可以只添加一个项目文件夹,将图像文件放入该文件夹并将其Build Action设置为Resource.例如,如果您调用该文件夹Images,则URI将是pack://application:,,,/Images/Red.jpg.