Convert BitmapImage to byte[]

Hi all

How can i convert a BitmapImage loaded with UriSource to a byte[ ] array so i can save it in SQLServer. I dont find how to convert to a Stream

Thanks.



Answer this question

Convert BitmapImage to byte[]

  • terronmax

    You need to copy out the pixel data using BitmapSource.CopyPixels.
  • Squire James

    What kind of overhead is incurred through this technique

    Let me provide an example. If you maintained a collection of classes which held raw image data in a byte[] property and wanted to display the image data for the currently selected item in the collection, under the prescribed technique, code-behind would be required to create an new instance of a BitmapSource class each time the current item is changed, regardless of if a collection item's image has been previously viewed.

    How expensive is the process to create the BitmapSource instance from the byte array, and subsequently convert the BitmapSource back to a byte[] to store an updated image in the DB

  • timbador

    On BitmapImage there is a CopyPixels method which will let you get the pixel data out in the form of an Array. Here you can pass it a byte[] and get what you need. Please keep in mind though that these are raw uncompressed image bits that are not format specific. So in order to use them in the future you need to use the BitmapSource.Create method to create a BitmapSource from an Array.



    Robert.

  • Convert BitmapImage to byte[]