4

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;
Arad Alvand
  • 8,607
  • 10
  • 51
  • 71
Jean AR
  • 41
  • 1
  • 1
  • 5
  • See this (there are some methods there): http://stackoverflow.com/questions/4427059/how-to-change-resolution-dpi-of-an-image – David BS Jul 14 '16 at 20:36

2 Answers2

16

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);
lilo.jacob
  • 2,334
  • 1
  • 20
  • 17
1

looking for the same solution, i found it in MSDN oficial blog by Microsoft.

CoreCompat.System.Drawing

If you have existing code relying on System.Drawing, using this library is clearly your fastest path to .NET Core and cross-platform bliss: the performance and quality are fine, and the API is exactly the same. The built-in System.Drawing APIs are the easiest way to process images with .NET Framework, but they rely on the GDI+ features from Windows, which are not included in .NET Core, and are a client technology that was never designed for multi-threaded server environments. There are locking issues that may make this solution unsuitable for your applications.

using System.Drawing;

const int size = 150;
const int quality = 75;

using (var image = new Bitmap(System.Drawing.Image.FromFile(inputPath)))
{
    int width, height;
if (image.Width > image.Height)
{
    width = size;
    height = Convert.ToInt32(image.Height * size / (double)image.Width);
}
else
{
    width = Convert.ToInt32(image.Width * size / (double)image.Height);
    height = size;
}
var resized = new Bitmap(width, height);
using (var graphics = Graphics.FromImage(resized))
{
    graphics.CompositingQuality = CompositingQuality.HighSpeed;
    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphics.CompositingMode = CompositingMode.SourceCopy;
    graphics.DrawImage(image, 0, 0, width, height);
    using (var output = File.Open(
        OutputPath(path, outputDirectory, SystemDrawing), FileMode.Create))
    {
        var qualityParamId = Encoder.Quality;
        var encoderParameters = new EncoderParameters(1);
        encoderParameters.Param[0] = new EncoderParameter(qualityParamId, quality);
        var codec = ImageCodecInfo.GetImageDecoders()
            .FirstOrDefault(codec => codec.FormatID == ImageFormat.Jpeg.Guid);
        resized.Save(output, codec, encoderParameters);
    }
}
}

reference: https://blogs.msdn.microsoft.com/dotnet/2017/01/19/net-core-image-processing/

Lucas Argate
  • 329
  • 2
  • 11