AdBrite

Your Ad Here

Friday, October 15, 2010

Converting a bitmap to a byte array

// Bitmap bytes have to be created via a direct memory copy of the bitmap
private byte[] BmpToBytes_MemStream (Bitmap bmp)
{
MemoryStream ms = new MemoryStream();
// Save to memory using the Jpeg format
bmp.Save (ms, ImageFormat.Jpeg);

// read to end
byte[] bmpBytes = ms.GetBuffer();
bmp.Dispose();
ms.Close();

return bmpBytes;
}
 //Bitmap bytes have to be created using Image.Save()
private Image BytesToImg (byte[] bmpBytes)
{
MemoryStream ms = new MemoryStream(bmpBytes);
Image img = Image.FromStream(ms);
// Do NOT close the stream!

return img;
}

No comments:

Post a Comment

BidVertiser

pocket cents

PocketCents Local Online Advertising