none
How to convert a Memory stream to a Bitmap

    Question

  • 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);
    Thursday, November 10, 2011 3:42 AM

Answers

  • 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
    • Proposed as answer by HeToC Thursday, November 10, 2011 11:58 AM
    • Marked as answer by Bob_BaoMVP Tuesday, November 22, 2011 7:37 AM
    Thursday, November 10, 2011 6:35 AM