0

how to resize an image without loosing its quality in c#.net?

Stream myBlob;
var encoder = new JpegEncoder();using (var output = new MemoryStream())
using (SixLabors.ImageSharp.Image<Rgba32> image = Image.Load(myBlob))
{   var divisor = image.Width / 100;
    var height = Convert.ToInt32(Math.Round((decimal)(image.Height/divisor)));
    image.Mutate(x => x.Resize(320, 640));
    image.Save(output, encoder);
    output.Position = 0;
    await blockBlob.UploadFromStreamAsync(output);
}
Johnny
  • 8,939
  • 2
  • 28
  • 33
  • 4
    Possible duplicate of [How to resize an Image C#](https://stackoverflow.com/questions/1922040/how-to-resize-an-image-c-sharp) – pix Jul 15 '19 at 08:38
  • 1
    I think you are asking how to do it with ImageSharp. Their documentation is shoddy at best, so your solution would be to look in the source code, specifically resamplers: https://github.com/SixLabors/ImageSharp/tree/master/src/ImageSharp/Processing/Processors/Transforms/Resamplers – Siderite Zackwehdex Jul 15 '19 at 09:50

1 Answers1

2

You can use the overloaded Bitmap constructor to create the new re-sized image, as can be seen bellow:

public static Image Resize(Image image, Size size)
{
   return (Image)(new Bitmap(image, size));
}

And you call it using:

var result = Resize(image, new Size(320,640));
leandro.andrioli
  • 997
  • 5
  • 12
  • Which, however, if the source file was already a JPEG would introduce additional artifacts when reencoded to JPEG and therefore still lose quality. – ckuri Jul 15 '19 at 08:52