Skip to content

Instantly share code, notes, and snippets.

@bleroy
Last active October 16, 2018 20:10
Show Gist options
  • Save bleroy/ed567ac14cbbc5439c41ce7ffff792d3 to your computer and use it in GitHub Desktop.
Save bleroy/ed567ac14cbbc5439c41ce7ffff792d3 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);
}
}
}
@CharlesToniolo
Copy link

Hi, the const quality should be long. If the quality is int, sometimes I get a invalid parameter error.

@dhavalsp
Copy link

dhavalsp commented Oct 9, 2018

Hi, the const quality should be long. If the quality is int, sometimes I get a invalid parameter error.

Thanks. It's help

@ranouf
Copy link

ranouf commented Oct 16, 2018

Hi,

That s a really example thanks!
Instead of saving the new picture in a file, I would like to send it to external storage (Azure in my case) using a stream, so I replaced:

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);
        }

by

using (var newMs = new MemoryStream())
                            {
                                var qualityParamId = Encoder.Quality;
                                var encoderParameters = new EncoderParameters(1);
                                encoderParameters.Param[0] = new EncoderParameter(qualityParamId, _imageCompressorSettings.Quality);
                                var codec = Array.Find(ImageCodecInfo.GetImageDecoders(), c => c.FormatID == ImageFormat.Jpeg.Guid);
                                resized.Save(newMs, codec, encoderParameters);
                                await _storageService.SaveStreamAsync(newMs, "test.jpg");
                            }

But the file "test.jpg" is always empty. Could you help me?

@ranouf
Copy link

ranouf commented Oct 16, 2018

i found the solution, I added this:
newMs.Position = 0
before:
await _storageService.SaveStreamAsync(newMs, "test.jpg");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment