2013-02-11 25 views
0

在窗口形式:如何像這個孔德轉換窗口形式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"))); 

回答

0

在WPF中可以直接使用鏈接作爲ImageSource

例子:

代碼:

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

Xaml:

<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> 

或者您可以按照您在winforms應用程序中的相同方式下載並顯示圖像。

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