1

I am creating a portfolio site for a photographer.

I am faced with the problem of resizing photos from large to small. If I reduce the size much of the quality is lost. How can I compress the picture in order not to lose quality?

My code:

using (var input = new Bitmap(imageFile.InputStream))
            {
                int width;
                int height;
                if (input.Width > input.Height)
                {
                    width = 411 * input.Width / input.Height;
                    height = 411;
                }
                else
                {
                    height = 411;
                    width = 411 * input.Width / input.Height;
                }

                using (var thumb = new Bitmap(width, height))
                using (var graphic = Graphics.FromImage(thumb))
                {
                    graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    graphic.SmoothingMode = SmoothingMode.AntiAlias;
                    graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;

                    graphic.DrawImage(input, 0, 0, width, height);
                    using (var output = System.IO.File.Create(imagePath))
                    {
                        thumb.Save(output, ImageFormat.Jpeg);
                    }
                }
            }
Dave New
  • 38,496
  • 59
  • 215
  • 394
Mediator
  • 14,951
  • 35
  • 113
  • 191

1 Answers1

2

You may get better results doing gradual resizing (like 10-25% each step). Try also saving in loss-less format (like ImageFormat.Png).

If quality is extremely important manual conversion in proper photo editing tool is probably the right approach.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179