locked
How to convert BitmapImage's source to a byteArray RRS feed

  • Question

  • I'm working on a Windows Store App that uses SQLite3 to save database. I finished building and testing my Database and its working fine. Everything is working well but I'm currently stuck in saving BitmapImage that I obtained from Image->Source.

    I understand that to initialize the BitmapImage I need to do BitmapImage->SetSource(Stream), But I cannot find a getSource method in Documentation of Bitmap Image. If such thing exist I would've expected it to return a Stream that I could've easily converted to a byte Array but since that is not the case, I'm currently on halt with my project. Any help would be appreciated.

    Sunday, January 26, 2014 2:45 AM

Answers

  • Use a WriteableBitmap instead of a BitmapImage if you need access to the pixels.

    • Proposed as answer by Michael Blome Friday, January 31, 2014 10:54 PM
    • Marked as answer by JoeyAndres Sunday, February 2, 2014 4:21 AM
    Sunday, January 26, 2014 2:38 PM
    Moderator

All replies

  • Use a WriteableBitmap instead of a BitmapImage if you need access to the pixels.

    • Proposed as answer by Michael Blome Friday, January 31, 2014 10:54 PM
    • Marked as answer by JoeyAndres Sunday, February 2, 2014 4:21 AM
    Sunday, January 26, 2014 2:38 PM
    Moderator
  • Hi

    You can use WriteableBitmap

        

    WriteableBitmap writeableBitmap = new WriteableBitmap(bitmapImage);
                Stream stream = writeableBitmap.PixelBuffer.AsStream();

                byte[] pixels = new byte[stream.Length];

                await stream.ReadAsync(pixels, 0, pixels.Length);

    Regards


    Varun Ravindranath

    Sunday, January 26, 2014 7:44 PM
  • Varun, this is a C++ forum. The Visual C++ compiler won't understand your example :-)

    Friday, January 31, 2014 10:52 PM
  • Hi Varun,

    Your sample code appears to be for WPF. It is not valid for Windows Store apps (even if, as Michael notes, it were converted to C++).

    There is no good way to recover the pixels from a BitmapImage. Instead the app will need to either reacquire from the original source or render into a RenderTargetBitmap. If you know you will need access to the pixels then you can load it into a WriteableBitmap instead of a BitmapImage.

    --Rob

    Saturday, February 1, 2014 7:11 AM
    Moderator