Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a simple stream(http webresponse) to bitmapimage in c# windows 8?

I try 1000 times, to convert a simple stream (http webresponse) to bitmapimage, but no one tutorial is working in c# windows 8.

Example:

BitmapImage image = new BitmapImage();
image.SetSource(stream);
image1.Source = image; 

Thank's for all reply.

Solution

InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
DataWriter writer = new DataWriter(randomAccessStream.GetOutputStreamAt(0));
writer.WriteBytes((byte[])command);
await writer.StoreAsync();
BitmapImage image = new BitmapImage();
image.SetSource(randomAccessStream);
like image 874
flatronka Avatar asked Dec 30 '11 09:12

flatronka


2 Answers

Have you tried this?

InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
DataWriter writer = new DataWriter(randomAccessStream.GetOutputStreamAt(0));
writer.WriteBytes(response.Content.ReadAsByteArray());
BitmapImage image = new BitmapImage();
image.SetSource(randomAccessStream);
like image 116
Mike Nakis Avatar answered Nov 14 '22 23:11

Mike Nakis


Try this code:

private async Task GetLocalImageAsync(string internetUri, string folderName, 
                                      string uniqueName)
{
    using (var response = await HttpWebRequest.CreateHttp(internetUri)
                                .GetResponseAsync())
    {
        using (var stream = response.GetResponseStream())
        {
            var folder = await ApplicationData.Current.LocalFolder
                               .CreateFolderAsync(folderName, 
                                        CreationCollisionOption.OpenIfExists);
            var file = await folder.CreateFileAsync(
                                    string.Format("{0}", uniqueName),
                                    CreationCollisionOption.ReplaceExisting);
            using (var filestream = await file.OpenStreamForWriteAsync())
            {
                await stream.CopyToAsync(filestream);
                await filestream.FlushAsync();
            }
        }
    }
}
like image 31
Vulcan Lee Avatar answered Nov 15 '22 00:11

Vulcan Lee