1

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

1
  • 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. Commented Oct 6, 2014 at 15:21

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Almost correct answer, I had the height wrong of the image, you mention the buffer size and that lead me to the correct solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.