C#: How to convert BITMAP byte array to JPEG format?

Marc picture Marc · Jan 19, 2009 · Viewed 49.2k times · Source

How can I convert a BITMAP in byte array format to JPEG format using .net 2.0?

Answer

Marc Gravell picture Marc Gravell · Jan 19, 2009

What type of byte[] do you mean? The raw file-stream data? In which case, how about something like (using System.Drawing.dll in a client application):

    using(Image img = Image.FromFile("foo.bmp"))
    {
        img.Save("foo.jpg", ImageFormat.Jpeg);
    }

Or use FromStream with a new MemoryStream(arr) if you really do have a byte[]:

    byte[] raw = ...todo // File.ReadAllBytes("foo.bmp");
    using(Image img = Image.FromStream(new MemoryStream(raw)))
    {
        img.Save("foo.jpg", ImageFormat.Jpeg);
    }