Answered How to convert a Memory stream to a Bitmap

  • Thursday, November 10, 2011 3:42 AM
     
     
    I use the code snipet below to create an image with bytes from server, it worked in Silverlight and WP7. But, how to do the same work in Windows 8? Any advice and suggestion will be appreciated. BitmapImage image = new BitmapImage(); var stream = new MemoryStream(response.ByteData); stream.Write(response.ByteData, 0, response.ByteData.Length); image.SetSource(stream);

All Replies

  • Thursday, November 10, 2011 6:35 AM
     
     Answered Has Code

    I think you don't need to read/write the stream, here's a snippet that reads a stream from a file:

    async internal void DoOpenFile(IReadOnlyList<Windows.Storage.IStorageItem> files)
          {
             StorageFile file = files[0] as StorageFile;
             IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read);         
             BitmapImage img = new BitmapImage();
             img.SetSource(readStream);
             detailImage.Source = img;
            }
    
    also have a look at this interesting post: http://www.charlespetzold.com/blog/2011/11/080203.html


    Corrado Cavalli [Microsoft .NET MVP-MCP]
    UGIdotNET - http://www.ugidotnet.org
    Weblog: http://blogs.ugidotnet.org/corrado/
    Twitter: http://twitter.com/corcav