0

I want to get an image from a user in Asp.net MVC and resize it to a specific size but I have to resize it to a certain size without deformation. i do not have problem if some part of the image cropped but I do not want if user uploads 4000X2000px image I crop 600X600 of it and lose many part of the image.

how can I do that? is there any algorithm according to that? or is there any source code in .net?

tereško
  • 58,060
  • 25
  • 98
  • 150
Hamed
  • 153
  • 2
  • 11

1 Answers1

0

I found the solution:

               using (var bitmap = new Bitmap(image.InputStream))
                       {
                           var width = bitmap.Width;
                           var height = bitmap.Height;


                           double widthRatio = (double)600 / width;
                           double heightRatio = (double)600 / height;
                           double ratio = widthRatio > heightRatio ? widthRatio : heightRatio;


                           var resizedWidth = width < height ? 600 : (int)(width * ratio);
                           var resizedHeight = height < width ? 600 : (int) (height * ratio);


                           var converter = new ImageConverter();
                           var image =
                      new WebImage((byte[])converter.ConvertTo(bitmap, typeof(byte[])))
                      .Resize(resizedWidth, resizedHeight)
                      .Crop((resizedHeight - 600) / 2, ((resizedWidth - 600)/ 2), ((resizedHeight - 600) / 2), ((resizedWidth - 600) / 2));
          }
Hamed
  • 153
  • 2
  • 11