Get Bytes from Bitmap Object

  • Thread starter Thread starter Guest
  • Start date Start date
Raed Sawalha said:
I have a Bitmap Object and I need to return it as array of bytes is it
possible

private static byte[] GetBitmapBytes(Bitmap Bitmap)
{
MemoryStream memStream = new MemoryStream();
byte[] bytes;

try {
// Save the bitmap to the MemoryStream.
Bitmap.Save(memStream, Bitmap.RawFormat);

// Create the byte array.
bytes = new byte[memStream.Length];

// Rewind.
memStream.Seek(0, SeekOrigin.Begin);

// Read the MemoryStream to get the bitmap's bytes.
memStream.Read(bytes, 0, bytes.Length);

// Return the byte array.
return bytes;
} finally {
// Cleanup.
memStream.Close();
}
}

HTH :)

Mythran
 
Back
Top