0

from examples I have been able to create BitmapImage the byte array

public byte[] BufferFromImage(BitmapImage myImageFile)
{
    WriteableBitmap btmMap = new WriteableBitmap(BitmapFactory.ConvertToPbgra32Format(myImageFile));
    return btmMap.ToByteArray();
}

Now I’m looking to reverse that, but so far without success , I’ve checked on https://writeablebitmapex.codeplex.com which states it can Create a WriteableBitmap from a byte array but I haven’t found any examples.

public WriteableBitmap ByteArrayToImage(Byte[] BArray)
{

    var width = 100;  
    var height = 100;  
    var dpiX = 96d;
    var dpiY = 96d;
    var pixelFormat = PixelFormats.Pbgra32;  
    var bytesPerPixel = (pixelFormat.BitsPerPixel + 7) / 8;
    var stride = bytesPerPixel * width;

    var bitmap = BitmapImage.Create(width, height, dpiX, dpiY, pixelFormat, null, BArray, stride);
    WriteableBitmap wbtmMap = new WriteableBitmap(BitmapFactory.ConvertToPbgra32Format(bitmap));
    return wbtmMap;
}

This returns an error

System.ArgumentException was unhandled by user code Message=Buffer size is not sufficient.

I hoping someone can point me in the right direction cheers

daniele3004
  • 13,072
  • 12
  • 67
  • 75
mgphall
  • 67
  • 3
  • 9
  • On what line does the exception originate? I assume it is either `BitmapImage.Create` or `new WriteableBitmap`, but that would be a good starting point. – Mike Strobel Oct 06 '14 at 15:21

1 Answers1

2

Try to increase the buffer's size... Buffer size should be calculated like stride * height.

cKNet
  • 635
  • 1
  • 10
  • 22
  • Almost correct answer, I had the height wrong of the image, you mention the buffer size and that lead me to the correct solution – mgphall Oct 06 '14 at 20:51