简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM