3

There are a few samples to do this but they are for Windows Phone 8.0 or 8.1 Silverlight.

But how can you do this for Windows Phone 8.1 Runtime?

Rob Caplan - MSFT
  • 21,714
  • 3
  • 32
  • 54
Bayern
  • 330
  • 3
  • 20
  • There http://stackoverflow.com/questions/4732807/conversion-of-bitmapimage-to-byte-array are some considerations about it. – marcinax Dec 09 '15 at 17:13
  • @marcinax did you read the title? Or what I posted? I know there are 100 samples and questions to do this... That link you posted is for Windows Phone """7"""!!!! – Bayern Dec 09 '15 at 17:15
  • So another link for you: http://stackoverflow.com/questions/12963508/bitmapimage-to-byte-windows-8-metro-winrt. It's for WinRT. Use search. – marcinax Dec 09 '15 at 17:16
  • @marcinax doesn't work. – Bayern Dec 09 '15 at 18:32

2 Answers2

7

You cannot extract the pixels from a Windows.UI.Xaml.Media.Imaging.BitmapImage.

The most general solution is to use a WriteableBitmap instead of a BitmapImage. These classes are both BitmapSources and can be used almost interchangeably. The WriteableBitmap provides access to its pixel data via its PixelBuffer property:

byte[] pixelArray = myWriteableBitmap.PixelBuffer.ToArray(); // convert to Array
Stream pixelStream = wb.PixelBuffer.AsStream();  // convert to stream

Otherwise you will need to acquire the pixels from wherever the BitmapImage got them from. Depending on how the BitmapImage was initialized you may be able to find its origin from its UriSource property. The WinRT Xaml Toolkit has an extension method FromBitmapImage to create a WriteableBitmap from an BitmapImage based on its UriSource.

An ugly option would be to render the BitmapImage into an Image, create a RenderTargetBitmap based on the Image and then get its Pixels with RenderTargetBitmap.CopyPixelsAsync()

Rob Caplan - MSFT
  • 21,714
  • 3
  • 32
  • 54
  • 1
    This is how I get and show the image: StorageFile storageFile = args.Files[0]; var stream = await storageFile.OpenAsync(FileAccessMode.Read); var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage(); await bitmapImage.SetSourceAsync(stream); var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream); – Bayern Dec 10 '15 at 09:42
  • I saw the very same code snippet now a few times in various places and wondered whether the pixel stream needs disposing? – thumbmunkeys Feb 16 '16 at 12:15
  • Had to add `using System.Runtime.InteropServices.WindowsRuntime;` to access the `AsStream()` extension method. – Felix May 22 '20 at 18:57
-1

Tried this?

private byte[] ConvertToBytes(BitmapImage bitmapImage)
{
    byte[] data = null;
    using (MemoryStream stream = new MemoryStream())
    {
        WriteableBitmap wBitmap = new WriteableBitmap(bitmapImage);
        wBitmap.SaveJpeg(stream, wBitmap.PixelWidth, wBitmap.PixelHeight, 0, 100);
        stream.Seek(0, SeekOrigin.Begin);
        data = stream.GetBuffer();
    }

    return data;
}
Depechie
  • 6,102
  • 24
  • 46
  • 2
    This won't work for a Windows Phone Runtime app since Windows.UI.Xaml.Media.Imaging.WriteableBitmap doesn't have a constructor which takes a BitmapImage. – Rob Caplan - MSFT Dec 10 '15 at 00:48
  • 4
    Nope, this ain't UWP code either. UWP _also_ does not have such a constructor. – Rhywden Dec 31 '15 at 23:20