开发者

How do you get from a byte[] or from a MemoryStream to a System.Windows.Controls.Image in WP7?

开发者 https://www.devze.com 2023-02-26 03:27 出处:网络
I need to make a GET request that requires oauth, for and image to display in a page. Is there a way to开发者_高级运维 do this without building a custom webrequest or httpwebrequest?If it\'s an image

I need to make a GET request that requires oauth, for and image to display in a page. Is there a way to开发者_高级运维 do this without building a custom webrequest or httpwebrequest?


If it's an image make sure that you are reading the stream as binary data.

Something like:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号