为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

如何用Properties.Resources中的图像从WPF中的代码隐藏动态更改图像源?我有一个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();     }}我倾向于在初始化期间转换一次图像,并保留图像源列表。我还假设需要一个依赖项属性来将控件绑定到,但我不知道如何使用以下图像源列表设置该属性:    // 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); }     }
查看完整描述

2 回答

?
jeck猫

TA贡献1909条经验 获得超7个赞

虽然WPF项目中的图像资源会生成System.Drawing.Bitmap财产Resources.Designer.cs,您可以直接创建一个BitmapImage从那个资源。您只需设置建立行动图像文件的Resource(而不是默认的None).

如果你有文件Red.jpgResources文件夹中的VisualStudio项目,创建BitmapImage如下所示。它使用一个WPF包URI.

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

如果你有Image控件在XAML中的某个位置声明如下:

<Image x:Name="image"/>

您可以简单地设置Source属性设置为以下代码中的BitmapImage:

image.Source = bitmap;

如果您希望设置Source属性,可以通过绑定创建string属性,该属性返回图像URI。字符串将自动转换为BitmapImage被内置的TypeConverter在WPF。

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

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

<Image Source="{Binding ImageUri}"/>

当然,您也可以声明该属性为类型。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); }}

并以同样的方式捆绑:

<Image Source="{Binding Image}"/>

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

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;

更新:毕竟,您的图像不需要是VisualStudio项目中的资源。您只需添加一个项目文件夹,将图像文件放入该文件夹,并将其构建操作设置为Resource..例如,如果您调用该文件夹Images,URI将是pack://application:,,,/Images/Red.jpg.


查看完整回答
反对 回复 2019-07-22
?
收到一只叮咚

TA贡献1821条经验 获得超4个赞

这应该也适用于PNG文件。也许您不应该将它们作为资源添加到您的项目中(请参阅我的编辑)。只需使用您最喜欢的图像工具创建文件,并将它们添加到项目中的文件夹中即可。看见这里关于接受。

查看完整回答
反对 回复 2019-07-22
  • 2 回答
  • 0 关注
  • 1073 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信