WPF Image display webp via BitMapImgae BeginInit UriSource EndInit in MVVM

 private void GenenerateBitMapImageViaUrl(string url)
{
    BitmapImage bmi = new BitmapImage();
    bmi.BeginInit();
    bmi.UriSource=new Uri(url, UriKind.RelativeOrAbsolute);
    bmi.EndInit();
    if(bmi.CanFreeze)
    {
        bmi.Freeze();
    }            
    img.Source= bmi;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

//xaml
<Window x:Class="WpfApp19.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:behavior="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:local="clr-namespace:WpfApp19"
        WindowState="Maximized"
        mc:Ignorable="d"
        Title="{Binding ImgUrl,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
        Height="450" Width="800">
    <behavior:Interaction.Triggers>
        <behavior:EventTrigger EventName="KeyDown">
            <behavior:CallMethodAction MethodName="Window_KeyDown" TargetObject="{Binding}"/>
        </behavior:EventTrigger>
    </behavior:Interaction.Triggers>
    <Grid>
        <Image x:Name="img"/>
    </Grid>
</Window>




//xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace WpfApp19
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var vm = new MainVM(img);
            this.DataContext = vm;
        }
    }

    public class MainVM : INotifyPropertyChanged
    { 
        public MainVM(Image imgValue)
        {
            img = imgValue;
            InitData();
        }

        private void InitData()
        {
            imgsList = new List<string>(System.IO.Directory.GetFiles(@"../../Images"));
            if(imgsList!=null && imgsList.Any())
            {
                imgsCount= imgsList.Count;
                ImgUrl = imgsList[0];
                GenenerateBitMapImageViaUrl(ImgUrl);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if(handler!=null)
            {
                handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }

        }

        private Image img { get; set; }
        private List<string> imgsList { get; set; }
        private int imgsCount { get; set; }

        private int imgIdx;
        public int ImgIdx
        {
            get
            {
                return imgIdx;
            }
            set
            {
                if(value!=imgIdx)
                {
                    imgIdx = value;
                    OnPropertyChanged(nameof(ImgIdx));
                }
            }
        }

        private string imgUrl;
        public string ImgUrl
        {
            get
            {
                return imgUrl;
            }
            set
            {
                if(value!=imgUrl)
                {
                    imgUrl = value;
                    OnPropertyChanged(nameof(ImgUrl));
                }
            }
        }

       

        public void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.C)
            {
                var msgResult = MessageBox.Show("Are you sure to exit?", "Exit", MessageBoxButton.YesNo,
                    MessageBoxImage.Question, MessageBoxResult.Yes);
                if (msgResult == MessageBoxResult.Yes)
                {
                    Application.Current.Shutdown();
                }
            }
            else if (e.Key == Key.Down)
            {
                if (++ImgIdx >= imgsCount)
                {
                    ImgIdx = 0;
                }
            }
            else if(e.Key==Key.Up)
            {
                if(--ImgIdx<0)
                {
                    ImgIdx=imgsCount-1;
                }
            }
            ImgUrl = imgsList[ImgIdx];
            GenenerateBitMapImageViaUrl(ImgUrl);
        }

        private void GenenerateBitMapImageViaUrl(string url)
        {
            BitmapImage bmi = new BitmapImage();
            bmi.BeginInit();
            bmi.UriSource=new Uri(url, UriKind.RelativeOrAbsolute);
            bmi.EndInit();
            if(bmi.CanFreeze)
            {
                bmi.Freeze();
            }            
            img.Source= bmi;
        }
    }
}

 

posted @ 2024-10-10 23:06  FredGrit  阅读(10)  评论(0编辑  收藏  举报