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); | |
} | |
} | |
} |
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,
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:
by
But the file "test.jpg" is always empty. Could you help me?