2012-04-24 78 views
0

在我的XAML,我有:Web客戶端負載JPG和GIF到圖像

<Image Height="150" HorizontalAlignment="Left" Margin="0,4,0,0" Name="imgLogo" Stretch="Fill" VerticalAlignment="Top" Width="417" /> 
<Image Height="343" HorizontalAlignment="Left" Margin="0,155,0,0" Name="imgPhoto" Stretch="Fill" VerticalAlignment="Top" Width="417" /> 

在後面的C#代碼,我有:

WebClient wcForLogo = new WebClient(); 
wcForLogo.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wcForLogo_DownloadStringCompleted); 
wcForLogo.DownloadStringAsync(new Uri("http://mySite/logo.gif")); 

WebClient wcForPhoto = new WebClient(); 
wcForPhoto.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wcForPhoto_DownloadStringCompleted); 
wcForPhoto.DownloadStringAsync(new Uri("http://mySite/photo.jpg")); 

但現在我不知道怎麼抓圖像並將其張貼在我創建的XAML控件中。

2個問題:

  1. 有沒有辦法給e.Result直接複製到圖片控件,或者我應該緩存圖像,並使用緩存爲源,或者我應該保存圖像隔離存儲,然後將其用作源,然後在完成後刪除圖像?無論哪種情況,你能告訴我如何用代碼?
  2. GIF和JPG的處理方式有何不同?如果是這樣,你能告訴我兩種不同的方式嗎?

回答

2

如果您只想顯示圖片,則不必使用WebClient。您可以將圖像源中直接設置開放的,並且控制將下載的護理:

imgLogo.Source = new BitmapImage(new Uri("images/yourPicture.png", UriKind.Relative)); 

注意,GIF不受Image控件的支持。您仍然可以使用ImageTools庫中的轉換器顯示它們:Display GIF in a WP7 application with Silverlight

+0

非常感謝鏈接!事實證明,這是最好的答案,因爲我不確定圖像會以什麼圖像格式出現,所以我遵循其中一篇博客文章的建議,而您的鏈接建議使用瀏覽器控件。 – 2012-04-24 11:35:02

0

如果你想在獨立存儲中使用,以節省這樣

  WebClient m_webClient = new WebClient(); 

      Uri m_uri = new Uri("http://URL"); 

      m_webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted); 

      m_webClient.OpenReadAsync(m_uri); 




     } 



    void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) 
    { 
     int count; 

     Stream stream = e.Result; 

     byte[] buffer = new byte[1024]; 


     using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 




      using (System.IO.IsolatedStorage.IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("IMAGES.jpg", FileMode.Create, isf)) 
      { 
       count = 0; 

       while (0 < (count = stream.Read(buffer, 0, buffer.Length))) 
       { 
        isfs.Write(buffer, 0, count); 
       } 

       stream.Close(); 
       isfs.Close(); 
      } 
     } 

要獲得的圖像格式isostore:

字節[]數據;

 using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 

      using (IsolatedStorageFileStream isfs = isf.OpenFile(uri, FileMode.Open, FileAccess.Read)) 
      { 
       data = new byte[isfs.Length]; 
       isfs.Read(data, 0, data.Length); 
       isfs.Close(); 
      } 

     } 


     MemoryStream ms = new MemoryStream(data); 

     BitmapImage bi = new BitmapImage(); 

     bi.SetSource(ms); 

If you give the image name as image then set the source as bi: 

     image.source = bi; 

如果你想直接添加

WebClient client = new WebClient(); 
    Stream stream = client.OpenRead(imageUrl); 
    Bitmap bitmap = new Bitmap(stream); 
    image.source = bitmap; 
+0

感謝您的出色反應!這也是回顧IsolatedStorage的一個很好的參考,但是我決定採用更簡單的解決方案,因爲我寧願不保存圖像。 我很想+1你的帖子,但我沒有代表這樣做。不過謝謝。 :D – 2012-04-24 11:31:20

+0

你希望你在這裏問什麼答案從網址獲取圖像,但你標記爲回答從本地內容獲取圖像的答覆。這是不好的 – 2012-04-24 11:34:24

+0

如果你沒有要求提供網址意味着然後問像這樣的問題男人改變你的問題的格式,而要求 – 2012-04-24 11:36:37

1
using System.Net; 
using System.IO; 
     private void Form1_Load(object sender, EventArgs e) 
     { 
      WebClient webclient = new WebClient(); 
      webclient.DownloadDataAsync(new Uri("http://mySite/logo.gif")); 
      webclient.DownloadDataCompleted += callback; 

     } 
     void callback(object sender,DownloadDataCompletedEventArgs e) 
     { 
      var ms = new MemoryStream(e.Result); 
      pictureBox1.Image = Image.FromStream(ms); 
     } 
+0

感謝您的迴應,但其他人的回答更健康。無論如何,我很想爲你的帖子+1正確的答案,但我沒有代表這樣做。不過謝謝。 :D – 2012-04-24 11:33:12

0

比方說,你的圖像控制被稱爲MyImage,你可以做到這一點從一個URL加載圖像:

MyImage.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("http://mySite/photo.jpg")); 

沒有必要爲了下載圖像而做所有的管道工程,框架已經爲你做了!

+0

您的解決方案對我而言比選擇的答案更適合我,但他添加了有關處理GIF的鏈接,這是一件大事。 我很想+1你的帖子,但我沒有代表這樣做。不過謝謝。 :D – 2012-04-24 11:30:13

+0

沒問題@Ali,我很高興你解決了你的問題! :) – 2012-04-24 11:36:23