WPF image show web picture via url and converter

<Window.Resources>
     <local:ImageUrlConverter x:Key="imgConverter"/>
 </Window.Resources>


 <Image Source="{Binding ImgUrl,Converter={StaticResource imgConverter}}"
        RenderOptions.BitmapScalingMode="HighQuality"/>


public class ImageUrlConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string url = value?.ToString();
        if (!string.IsNullOrWhiteSpace(url))
        {
            WebClient client = new WebClient();
            var imgBytes = client.DownloadData(url);
            if (imgBytes != null && imgBytes.Any())
            {
                MemoryStream ms = new MemoryStream(imgBytes);
                ms.Seek(0, SeekOrigin.Begin);
                BitmapImage bmi = new BitmapImage();
                bmi.BeginInit();
                bmi.StreamSource = ms;
                bmi.EndInit();
                if (bmi.CanFreeze)
                {
                    bmi.Freeze();
                }
                return bmi;
            }
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

 

 

 

 

 

//xaml
<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        WindowState="Maximized"
        WindowStyle="None"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:MainVM/>
    </Window.DataContext>
    <Window.Resources>
        <local:ImageUrlConverter x:Key="imgConverter"/>
    </Window.Resources>
    <behavior:Interaction.Triggers>
        <behavior:EventTrigger EventName="MouseDown">
            <behavior:InvokeCommandAction 
                Command="{Binding MouseDownCommand}"
                CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"/>
        </behavior:EventTrigger>
    </behavior:Interaction.Triggers>    
    <Grid>
        <Image Source="{Binding ImgUrl,Converter={StaticResource imgConverter}}"
               RenderOptions.BitmapScalingMode="HighQuality"/>
    </Grid>
</Window>



//cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
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;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class MainVM : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public string ImgUrl { get; set; } = "https://wx4.sinaimg.cn/mw690/007ucPXmgy1hteb8tr7gjj311x1kwhdt.jpg";

        public DelCmd MouseDownCommand { get; set; }

        public MainVM()
        {
            InitCommands();
        } 

        private void InitCommands()
        {
            MouseDownCommand = new DelCmd(MouseDownCommandExecuted);
        }

        private void MouseDownCommandExecuted(object obj)
        {
            var win = obj as Window;
            if (win != null)
            {
                var pt = Mouse.GetPosition(win);
                MessageBox.Show($"X:{pt.X}\nY:{pt.Y}", "Location", MessageBoxButton.OK);
            }
        }

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

    public class ImageUrlConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string url = value?.ToString();
            if (!string.IsNullOrWhiteSpace(url))
            {
                WebClient client = new WebClient();
                var imgBytes = client.DownloadData(url);
                if (imgBytes != null && imgBytes.Any())
                {
                    MemoryStream ms = new MemoryStream(imgBytes);
                    ms.Seek(0, SeekOrigin.Begin);
                    BitmapImage bmi = new BitmapImage();
                    bmi.BeginInit();
                    bmi.StreamSource = ms;
                    bmi.EndInit();
                    if (bmi.CanFreeze)
                    {
                        bmi.Freeze();
                    }
                    return bmi;
                }
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class DelCmd : ICommand
    {
        public event EventHandler CanExecuteChanged
        {
            add
            {
                CommandManager.RequerySuggested += value;
            }
            remove
            {
                CommandManager.RequerySuggested -= value;
            }
        }

        private Action<object> execute;
        private Predicate<object> canExecute;

        public DelCmd(Action<object> executeValue, Predicate<object> canExecuteValue)
        {
            this.execute = executeValue;
            this.canExecute = canExecuteValue;
        }

        public DelCmd(Action<object> executeValue) : this(executeValue, null)
        {

        }

        public bool CanExecute(object parameter)
        {
            if (canExecute == null)
            {
                return true;
            }
            return canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            execute(parameter);
        }
    }
}

 

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