2

I am creating thumbnail of Image file but it gives very poor quality image. please suggest me the way to improve the quality of thumbnail. my function is given below-

 public Image RezizeI(Image img, int maxW, int maxH)
    {
        if (img.Height < maxH && img.Width < maxW) return img;
        using (img)
        {
            Double xRatio = (double)img.Width / maxW;
            Double yRatio = (double)img.Height / maxH;
            Double ratio = Math.Max(xRatio, yRatio);
            int nnx = (int)Math.Floor(img.Width / ratio);
            int nny = (int)Math.Floor(img.Height / ratio);
            Bitmap cpy = new Bitmap(nnx, nny, PixelFormat.Format32bppArgb);
            using (Graphics gr = Graphics.FromImage(cpy))
            {
                gr.Clear(Color.Transparent);

                // This is said to give best quality when resizing images
                gr.InterpolationMode = InterpolationMode.HighQualityBicubic;

                gr.DrawImage(img,
                    new Rectangle(0, 0, nnx, nny),
                    new Rectangle(0, 0, img.Width, img.Height),
                    GraphicsUnit.Pixel);
            }
            return cpy;
        }
    }

for example my original image is enter image description here

and created thumbnail is- enter image description here

Vivek Mishra
  • 1,772
  • 1
  • 17
  • 37
  • by poor quality do you mean the change in W:H ratio? – Ňɏssa Pøngjǣrdenlarp Feb 21 '15 at 15:21
  • no that's not the problem. the problem is quality i.e. the image looking like made of small dots. I dont know what it is exactly called may be the pixels. as you are seeing in thumb image. – Vivek Mishra Feb 21 '15 at 15:25
  • no same problem is occuring – Vivek Mishra Feb 21 '15 at 15:45
  • First, it looks like you have different proportions for the thumbnail. Second, expect a lower quality image when reducing the size of the image. A thumbnail is what it is, a thumbnail. You are supposed to show a thumbnail on a smaller screen area where the reduction in image quality will not be relevant. – Tarik Feb 21 '15 at 15:46
  • 1
    Try the other properties that are used [in the non-accepted answer](http://stackoverflow.com/a/2324414/578411) where you copied that on that same question you got your code from... – rene Feb 21 '15 at 16:13
  • @rene I tried that code also but the same error persists. – Vivek Mishra Feb 21 '15 at 17:01
  • ok now I got what the problem is. when I store these thumbs locally, they are perfect but when I save them as a blob on azure blob storage, the quality degrades. but the real image quality remains same. Please help me regarding this. – Vivek Mishra Feb 22 '15 at 06:26
  • I have managed it to upload asynchronously on azure. now the thumbnails are perfect. Thank you everyone for your support. – Vivek Mishra Feb 22 '15 at 08:32

1 Answers1

0

I can't guarantee it, but the code I've used in the past to create better quality thumbnails includes these two lines...

gr.SmoothingMode = Drawing2D.SmoothingMode.HighQuality;
gr.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality;
freefaller
  • 19,368
  • 7
  • 57
  • 87