Last active
October 16, 2018 20:10
-
-
Save bleroy/ed567ac14cbbc5439c41ce7ffff792d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Hi, the
const
quality should belong
. If the quality isint
, sometimes I get a invalid parameter error.
Thanks. It's help
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?
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
Hi, the
const
quality should belong
. If the quality isint
, sometimes I get a invalid parameter error.