Is it possible to convert image formats using the SixLabors.ImageSharp Libraries?

4.8k views Asked by At

I have been studying this library with the intent to make a image processing pipeline in C#. I would want it to be able to receive a .png image and convert it to a .jpeg. Yet I have not found a way to do this using this library. Is it even possible? Has anyone been able to do this successfully?

This was my best shot:

Image<Rgba32> image1 = SixLabors.ImageSharp.Image.Load<Rgba32>(fileName, out IImageFormat format);

var encd = new JpegEncoder
                {
                    Quality = 80
                };

image.Save(outputFinalPath, encd);

The result is the image simply being saved as a .png. I'm new to working with images so I might be missing something.

1

There are 1 answers

3
tocsoft On BEST ANSWER

all you have to do to load an image from any PNG file then call save passing a path to a file with a jpg file extension.

using (var img = Image.Load("path/to/inputImage.png"))
{
    img.Save("path/to/outputImage.jpeg");
}

optionally passing a jpeg encoder to tweak settings, or you could also just change the defaults using the below code

Configuration.Default.ImageFormatsManager.SetEncoder(JpegFormat.Instance, new JpegEncoder()
{
    Quality = 90
});