Load an image into a byte-array with C#

Load an image into a byte-array with C#

This pretty simply snippet exports your Image into a byte-array.

public static byte[] ToByteArray(this System.Drawing.Image image)
{
    using(MemoryStream memoryStream = new MemoryStream())
    {
        image.Save(memoryStream);
        return memoryStream.ToArray();
    }
}

Usage:

System.Drawing.Image myImage = .....

byte[] imageAsByteArray = myImage.ToByteArray();

Hint: ImageSharp

ImageSharp uses the same signature to export bytes into a MemoryStream

    SixLabors.ImageSharp.Image myImage = ...
    using(MemoryStream memoryStream = new MemoryStream())
    {
        myImage.Save(outStream);
        return memoryStream.ToArray();
    }