2011-04-18 30 views
0

我需要做一個需要oauth的GET請求,並在圖像中顯示一個頁面。有沒有辦法做到這一點,而不建立一個自定義webrequest或httpwebrequest?你如何從一個字節[]或從一個MemoryStream中獲取WP7中的System.Windows.Controls.Image?

+0

你爲什麼要問這個問題兩次? – 2011-04-18 19:58:57

+0

[HTTPWebRequest GET with a OAuth Header for Windows Phone上的圖像]的可能重複(http://stackoverflow.com/questions/5699572/httpwebrequest-get-with-oauth-header-for-an-image-on-windows-電話) – 2011-04-18 19:59:23

回答

0

如果是圖像,請確保您正在以二進制數據的形式讀取數據流。

喜歡的東西:

var httpRequest = (HttpWebRequest)ar.AsyncState; 
var httpResponse = (HttpWebResponse)httpRequest.EndGetResponse(ar); 

int lengthInBytes = Convert.ToInt32(httpResponse.ContentLength); 
BinaryReader br = new BinaryReader(httpResponse.GetResponseStream()); 
byte[] imageInBytes = new byte[lengthInBytes]; 
using (br) 
{ 
    for (int i = 0; i < lengthInBytes; i++) 
    { 
     imageInBytes[i] = br.ReadByte(); 
    } 
} 

DispatcherHelper.CheckBeginInvokeOnUI(() => 
{ 
    MemoryStream rawBytesStream = new MemoryStream(imageInBytes); 
    BitmapImage img = new BitmapImage(); 

    img.SetSource(rawBytesStream); 
    imageInUI.Source = img; 
}); 
相關問題