1

In my program user can upload an image upto specific size and after that i am resizing an image. It works great if image is big , but if image is small and I try to resize to big image get blurred (basically its just zooming instead of resizing)...Can someone help how can I do that.Thanks!!

Here is my program

 public static Image ScaleBySize(Image imgPhoto, int size)
        {
            int logoSize = size;

            float sourceWidth = imgPhoto.Width;
            float sourceHeight = imgPhoto.Height;
            float destHeight = 0;
            float destWidth = 0;
            int sourceX = 0;
            int sourceY = 0;
            int destX = 0;
            int destY = 0;




            if (sourceWidth > size || sourceWidth < size) { destWidth = size; }
            if (sourceHeight > size || sourceHeight < size) { destHeight = size; }


            Bitmap bmPhoto = new Bitmap((int)destWidth, (int)destHeight,PixelFormat.Format32bppPArgb);
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
            grPhoto.DrawImage(imgPhoto,new Rectangle(destX, destY, (int)destWidth, (int)destHeight),new Rectangle(sourceX, sourceY, (int)sourceWidth, (int)sourceHeight),GraphicsUnit.Pixel);
            grPhoto.Dispose();

            return bmPhoto;
        }
1
  • Your best bet would be to leave an image smaller than your required size the same size. Only resize downward to eliminate distortion
    – Tommy
    Apr 17, 2013 at 16:01

1 Answer 1

2

Of course you large image will be blurred since there is no detail in the small image. The program is only "stretching" each pixel to fill a larger area.

4
  • Is there any way to do it...??
    – user207888
    Apr 17, 2013 at 15:37
  • @user207888 - no, there isn't. There is only so much information in an image of a given size. Taking a 50px x 50px image and trying to make it fill 200px x 200px will cause it to distort as each pixel is now essentially made into 16 pixels - major distortion. Only in CSI can you blow up a grainy image and have it as clear as the original :)
    – Tommy
    Apr 17, 2013 at 15:59
  • Oh K, may be i then just center the small image and resize the big images..Thanks!!
    – user207888
    Apr 17, 2013 at 16:03
  • This is why the "Zoom! Enhance image!" silliness in movies is so jarring. Just can't be done reasonably well (nothing from nothing leaves nothing). Apr 17, 2013 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.