3

Who can help me. I don't understand how i can convert BitmapImage or IRandomAccessStream to byte array. I try:

foreach (StorageFile file in files)
{
    BitmapImage src = new BitmapImage();

    using (IRandomAccessStream stream = await file.OpenReadAsync())
    {
        await src.SetSourceAsync(stream);

        WriteableBitmap bitMap = new WriteableBitmap(src.PixelWidth, src.PixelHeight);
        await bitMap.SetSourceAsync(stream);
    }
}

then i have WriteableBitmap and try this:

private byte[] ImageToByeArray(WriteableBitmap wbm)
{
    using (Stream stream = wbm.PixelBuffer.AsStream())
    using (MemoryStream memoryStream = new MemoryStream())
    {
        stream.CopyTo(memoryStream);
        return memoryStream.ToArray();
    }
}

but it's don't work for me ;(

Halvor Holsten Strand
  • 19,829
  • 17
  • 83
  • 99
goodniko
  • 163
  • 3
  • 14

2 Answers2

10

This should do it:

    async Task<byte[]> Convert(IRandomAccessStream s)
    {
        var dr = new DataReader(s.GetInputStreamAt(0));
        var bytes = new byte[s.Size];
        await dr.LoadAsync((uint)s.Size);
        dr.ReadBytes(bytes);
        return bytes;
    }
Jon
  • 2,891
  • 2
  • 17
  • 15
-1

I used this solution in my WPF applications to save images in database as byte[]. It should also work in your case.

public static byte[] ImageToString(System.Windows.Media.Imaging.BitmapImage img) {
    System.IO.MemoryStream stream = new System.IO.MemoryStream();
    System.Windows.Media.Imaging.BmpBitmapEncoder encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();
    encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create((System.Windows.Media.Imaging.BitmapSource)img));
    encoder.Save(stream);
    stream.Flush();

    return stream.ToArray();
}
erikscandola
  • 2,854
  • 2
  • 17
  • 24
  • not work ;( in windows 10 uap doesn't has this classes – goodniko Jan 03 '16 at 22:52
  • You cannot import System.Windows.Media.Imaging reference? – erikscandola Jan 03 '16 at 22:57
  • Have you tried these three solutions? [Solution1](http://stackoverflow.com/questions/6597676/bitmapimage-to-byte) - [Solution2](http://stackoverflow.com/questions/4732807/conversion-of-bitmapimage-to-byte-array) - [Solution3](http://stackoverflow.com/questions/7350679/convert-a-bitmap-into-a-byte-array) – erikscandola Jan 03 '16 at 23:02
  • yes, it's don't work too..there is no classes i also try: var decoder = await BitmapDecoder.CreateAsync(stream); UInt32 width = decoder.PixelWidth; UInt32 height = decoder.PixelHeight; var pixels = await decoder.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, new BitmapTransform(), ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb); return pixels.DetachPixelData(); – goodniko Jan 03 '16 at 23:18
  • How you get images? [Here](https://social.msdn.microsoft.com/Forums/windowsapps/en-US/2b75e17a-dce8-44aa-bce2-ccf5cce1a0f9/how-to-convert-a-image-to-byte-array?forum=wpdevelop) a possible solution if you use a path to images. – erikscandola Jan 03 '16 at 23:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99632/discussion-between-erikscandola-and-user3588235). – erikscandola Jan 03 '16 at 23:38