C# convert a BitMap to Byte[]

/// <summary>
/// Covert a bitmap to a byte array
/// </summary>
/// <param name="bitmap"></param>
/// <returns>
/// byte array, when bitmap could be converted
/// null, when bitmap is null
/// null, when bitmap could not be converted to byte array
/// </returns>
public byte[] ConvertBitMapToByteArray(Bitmap bitmap)
{
byte[] result = null;
if (bitmap != null)
{
MemoryStream stream = new MemoryStream();
bitmap.Save(stream, bitmap.RawFormat);
result = stream.ToArray();
}
return result;
}

2 Comments

  1. joan

    I’ trying this but i have this exception when attempt to fill a private byte[] field with the return of this method, can you help me?

    System.StackOverflowException

Leave a Reply

Your email address will not be published. Required fields are marked *