你如何从一个byte []或从MemoryStream到WP7中的System.Windows.Controls.Image?

时间:2011-04-18 09:25:28

标签: windows windows-phone-7

我需要发出一个GET请求,要求oauth,for和image在页面中显示。有没有办法在不构建自定义webrequest或httpwebrequest的情况下执行此操作?

1 个答案:

答案 0 :(得分: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;
});