亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何用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條經驗 獲得超5個贊

這應該也適用于PNG文件。也許您不應該將它們作為資源添加到您的項目中(請參閱我的編輯)。只需使用您最喜歡的圖像工具創建文件,并將它們添加到項目中的文件夾中即可??匆?/trans>這里關于接受。

查看完整回答
反對 回復 2019-07-22
  • 2 回答
  • 0 關注
  • 1235 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號