Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compressing Images in c# asp.net Core

Does anyone know how to compress images of any type (jpg, png, gif, etc.) in c# asp.net core. WebP doesn't work on core. I downloaded the ImageProcessor Core library, but can't figure out how to compress/reduce the quality of the image. Here's what we tried, but it didn't work.

    newImage.Quality = old_image.Quality-20;
    newImage.HorizontalResolution = 0;
    newImage.VerticalResolution = 0;
like image 614
Jean AR Avatar asked Dec 08 '22 21:12

Jean AR


1 Answers

We use Magic.Net to compress jpeg, gif and png images. It supports .Net Core, available via Nuget.

Example:

var file = new FileInfo(fileName);

Console.WriteLine("Bytes before: " + file.Length);

var optimizer = new ImageOptimizer();
optimizer.Compress(file);

file.Refresh();
Console.WriteLine("Bytes after:  " + file.Length);
like image 150
lilo.jacob Avatar answered Dec 23 '22 22:12

lilo.jacob