internal static Image deSerialiseBitmap(byte[] bytes)
{
GDIBitmap gdiBitmap = new GDIBitmap();
gdiBitmap.bmType = getIntFromByteArray(bytes, 0);
gdiBitmap.bmWidth = getIntFromByteArray(bytes, 4);
gdiBitmap.bmHeight = getIntFromByteArray(bytes, 8);
gdiBitmap.bmWidthBytes = getIntFromByteArray(bytes, 12);
gdiBitmap.bmPlanes = getShortFromByteArray(bytes, 16);
gdiBitmap.bmBitsPixel = getShortFromByteArray(bytes, 18);
gdiBitmap.bmBits = getIntFromByteArray(bytes, 20);
//
// from
// http://stackoverflow.com/questions/742236/how-to-create-a-bmp-file-from-byte-in-c-sharp
//
unsafe
{
fixed (byte* ptr = bytes)
{
int stride = gdiBitmap.bmWidth * 3;
//
// the new Bitmap(new Bitmap is to get a new copy of the bitmap)
//
// first we create a bitmap from the bytes array but this array will be garbage collected in the future
// so we make a deep copy and then refresh the unsafe bitmap
//
Bitmap unsafeBitmap = new Bitmap(gdiBitmap.bmWidth, gdiBitmap.bmHeight, -stride, PixelFormat.Format24bppRgb, new IntPtr(ptr + 24 + stride * (gdiBitmap.bmHeight - 1)));
Bitmap safeBitmap = new Bitmap(unsafeBitmap);
unsafeBitmap.Dispose();
return safeBitmap;
}
}
}