user207888
user207888

Reputation: 397

Not able to resize a small image to larger image

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;
        }

Upvotes: 1

Views: 378

Answers (1)

Tianyun Ling
Tianyun Ling

Reputation: 1097

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.

Upvotes: 2

Related Questions