locked
Convert BitmapImage to byte[]

    Question

  • I'm trying to convert a BitmapImage to a byte[] array in a Universal App. I have this:

            public static Byte[] BitmapImageToByte(this BitmapImage imageSource)
            {
                Stream stream = imageSource.StreamSource; //error here
                Byte[] buffer = null;
                if (stream != null && stream.Length > 0)
                {
                    using (BinaryReader br = new BinaryReader(stream))
                    {
                        buffer = br.ReadBytes((Int32)stream.Length);
                    }
                }
                return buffer;
            }

    But I'm getting the error:

    Windows.UI.Xaml.Media.Imaging.BitmapImage' does not contain a definition for 'StreamSource'
    and no extension method 'StreamSource' accepting a first argument of type
    'Windows.UI.Xaml.Media.Imaging.BitmapImage' could be found (are you missing a using directive
    or an assembly reference?)

    Monday, January 26, 2015 9:07 AM

Answers

  • There isn't any way to get the pixels out of a BitmapImage (short of rendering to a new bitmap with RenderTargetBitmap). If possible, use a WriteableBitmap instead of a BitmapImage. They are both ImageSources and work essentially the same other than WriteableBitmap giving access to its PixelBuffer.

    You can then get the raw pixels from the WriteableBitmap's PixelBuffer and encode them to a bitmap format (jpg, png, etc.) with a BitmapEncoder.

    Monday, January 26, 2015 6:55 PM
    Owner

All replies

  • Hi!

    In windows store apps BitmapImage does not have a StreamSource property. You can achieve what you want like this:

    using System.Runtime.InteropServices.WindowsRuntime;
    
    ...
    
    byte[] buffer = null; 
    
    using (MemoryStream ms = new MemoryStream())
        {
            WriteableBitmap wb = new WriteableBitmap(imageSource);
            Stream s1 = wb.PixelBuffer.AsStream();
            s1.CopyTo(ms);
    
            buffer = ms.ToArray();
        }
    
    

    Cheers,

    Sam.

    Monday, January 26, 2015 11:40 AM
  • Thanks Sam, but I get a

    'Windows.UI.Xaml.Media.Imaging.WriteableBitmap' does not contain a constructor that takes 1 arguments error

    on WriteableBitmap. Also, I can't add a reference to System.Runtime.InteropServices.WindowsRuntime.

    Monday, January 26, 2015 2:21 PM
  • There isn't any way to get the pixels out of a BitmapImage (short of rendering to a new bitmap with RenderTargetBitmap). If possible, use a WriteableBitmap instead of a BitmapImage. They are both ImageSources and work essentially the same other than WriteableBitmap giving access to its PixelBuffer.

    You can then get the raw pixels from the WriteableBitmap's PixelBuffer and encode them to a bitmap format (jpg, png, etc.) with a BitmapEncoder.

    Monday, January 26, 2015 6:55 PM
    Owner
  • using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
                {
                   
                  

                    var image = new BitmapImage();
                   ;
                    image.SetSource(ms);
                   
                    WriteableBitmap wb = new WriteableBitmap(image.PixelWidth, image.PixelHeight);
                    ms.Seek(0);
                    wb.SetSource(ms);
                    imagenProceso.Bmp = image;
                 
                    imagenProceso.Datos = wb.ToByteArray();
                    imagenProceso.Height =(uint) wb.PixelHeight;
                    imagenProceso.Width = (uint)wb.PixelWidth;
                    return imagenProceso;
                }

    Monday, January 26, 2015 9:00 PM