Skip to content

Instantly share code, notes, and snippets.

@VladoMS
Forked from bleroy/CoreCompat.System.Drawing.cs
Created February 1, 2017 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VladoMS/1b71f39d151ce74e3fe49d86bf48b770 to your computer and use it in GitHub Desktop.
Save VladoMS/1b71f39d151ce74e3fe49d86bf48b770 to your computer and use it in GitHub Desktop.
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment