Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将窗口格式的图像转换为WPF_Wpf - Fatal编程技术网

如何将窗口格式的图像转换为WPF

如何将窗口格式的图像转换为WPF,wpf,Wpf,这是窗户形式的康德: using (WebClient ownPicLoader = new WebClient()) pbOwnImage.Image = Image.FromStream(new MemoryStream(ownPicLoader.DownloadData("https://graph.facebook.com/" + _client.ClientNick + "/picture?width=200&height=200"))); 在WPF中,您可以直接使用链接作为

这是窗户形式的康德:

using (WebClient ownPicLoader = new WebClient())
pbOwnImage.Image = Image.FromStream(new MemoryStream(ownPicLoader.DownloadData("https://graph.facebook.com/" + _client.ClientNick + "/picture?width=200&height=200")));

在WPF中,您可以直接使用链接作为
ImageSource

例如:

代码:

Xaml:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent(); 
    }

    private string image;
    public string Image
    {
        get { return image; }
        set { image = value; NotifypropertyChanged("Image"); }
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        Image = "http://icons.iconarchive.com/icons/chromatix/keyboard-keys/128/0-icon.png";
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifypropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
 }
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="252.351" Width="403.213" Name="UI" >
    <Grid>
        <StackPanel HorizontalAlignment="Left" Height="177" Margin="23,10,0,0" VerticalAlignment="Top" Width="191">
            <Image Source="{Binding ElementName=UI, Path=Image, IsAsync=True}" Stretch="Uniform" />
            <Button Click="Button_Click_1" Content="Get Image" />
        </StackPanel>
    </Grid>
</Window>
public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent(); 
    }

    private BitmapImage image;
    public BitmapImage Image
    {
        get { return image; }
        set { image = value; NotifypropertyChanged("Image"); }
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        using (WebClient ownPicLoader = new WebClient())
        {
            ownPicLoader.DownloadDataCompleted += (s, args) =>
            {
                var downloadImage = new BitmapImage();
                downloadImage.BeginInit();
                downloadImage.StreamSource = new MemoryStream(args.Result);
                downloadImage.EndInit();
                Image = downloadImage;
            };
            ownPicLoader.DownloadDataAsync(new Uri("http://icons.iconarchive.com/icons/chromatix/keyboard-keys/128/0-icon.png"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifypropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
 }