locked
Bitmap Source from memory

    Question

  • Hi, how to create a bitmap source from a memory buffer, I need an IWICBitmapSource  and not an IWICBitmap, for performance reasons I don't want copy the memory buffer.

    Do i need to implement my own custom bitmap source (any Sample links ?) or is there an easier way ?

    thanks.

    Tuesday, February 18, 2014 1:11 PM

Answers

  • You may want to look at the source for DirectXTex which makes extensive use of WIC.

    Typically if you want to create a WIC bitmap object using your own buffer, you'd use CreateBitmapFromMemory

    Wednesday, February 19, 2014 6:03 AM
  • For the moment I didn't find a better solution than using CreateBitmapFromMemory but this function involves a copy from the buffer to the bitmap, the same function with the WICBitmapNoCache flag would have been perfect.

    Thanks.
    Wednesday, February 19, 2014 11:51 AM

All replies

  • private BitmapImage GetImageSource(byte[] imageBytes)
    {
        using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
        {
            using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
            {
                writer.WriteBytes(imageBytes);
                writer.StoreAsync().GetResults();
            }
     
            var image = new BitmapImage();
            image.SetSource(ms);
            return image;
        }
    }
    

    Tuesday, February 18, 2014 4:16 PM
  • Thanks for your answer.. but It's not a XAML project, I don't work with the BitmapSource class, but with the underlying native interface IWICBitmapSource from the WIC API.
    Tuesday, February 18, 2014 5:24 PM
  • You may want to look at the source for DirectXTex which makes extensive use of WIC.

    Typically if you want to create a WIC bitmap object using your own buffer, you'd use CreateBitmapFromMemory

    Wednesday, February 19, 2014 6:03 AM
  • For the moment I didn't find a better solution than using CreateBitmapFromMemory but this function involves a copy from the buffer to the bitmap, the same function with the WICBitmapNoCache flag would have been perfect.

    Thanks.
    Wednesday, February 19, 2014 11:51 AM