1

My requirement is to resize an image if it is bigger than 800 X 600 pixels. I have to resize it to 800 X 600 resolution as per aspect ratio.

I am using below code to re-size it:

public string ResizeUserImage(string fullFileName, int maxHeight, int maxWidth, string newFileName)
{
        string savepath = System.Web.HttpContext.Current.Server.MapPath("//InitiativeImage//");

        try
        {
            using (Image originalImage = Image.FromFile(fullFileName))
            {
                int height = originalImage.Height;
                int width = originalImage.Width;
                int newHeight = maxHeight;
                int newWidth = maxWidth;

                if (height > maxHeight || width > maxWidth)
                {
                    if (height > maxHeight)
                    {
                        newHeight = maxHeight;
                        float temp = ((float)width / (float)height) * (float)maxHeight;
                        newWidth = Convert.ToInt32(temp);

                        height = newHeight;
                        width = newWidth;
                    }

                    if (width > maxWidth)
                    {
                        newWidth = maxWidth;
                        float temp = ((float)height / (float)width) * (float)maxWidth;
                        newHeight = Convert.ToInt32(temp);
                    }

                    Image.GetThumbnailImageAbort abort = new Image.GetThumbnailImageAbort(ThumbnailCallback);
                    using (Image resizedImage = originalImage.GetThumbnailImage(newWidth, newHeight, abort, System.IntPtr.Zero))
                    {
                        //When image is compress then store the image

                        var guid = Guid.NewGuid().ToString();
                        string latestFileName = guid + Path.GetExtension(newFileName);

                        resizedImage.Save(savepath + @"\" + latestFileName);

                        string finalPath = AppSettings.Domain + "InitiativeImage/" + latestFileName;

                        return finalPath;

                    }


                }
                else if (fullFileName != newFileName)
                {

                    //var guid = Guid.NewGuid().ToString();
                    //newFileName = guid + Path.GetExtension(newFileName);

                    //// no resizing necessary, but need to create new file 
                    //originalImage.Save(savepath + @"\" + newFileName);

                    //string finalPath = AppSettings.Domain + "UserImage/" + newFileName;

                    return newFileName;
                }


                return fullFileName;
            }

        }
        catch (Exception)
        {

            throw;
        }
    }

It works perfect but resizing image gets damaged in pixel.

See below original image which I am uploading to the server:

Original image

After re-sizing it, it is damages like below image:

re-sized image

Hope you got my question. The resized image get damage in pixel. Please let me know where I am wrong or where is the issue.

Sachin
  • 152
  • 1
  • 17
Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188

2 Answers2

3

Pls try out below function.

public void FixedSize(string oldImageFile, int Width, int Height,string finalpath)
    {
        Bitmap imgPhoto = new Bitmap(oldImageFile);
        int sourceWidth = imgPhoto.Width;
        int sourceHeight = imgPhoto.Height;
        int sourceX = 0;
        int sourceY = 0;
        int destX = 0;
        int destY = 0;

        float nPercent = 0;
        float nPercentW = 0;
        float nPercentH = 0;

        nPercentW = ((float)Width / (float)sourceWidth);
        nPercentH = ((float)Height / (float)sourceHeight);
        if (nPercentH < nPercentW)
        {
            nPercent = nPercentH;
            destX = System.Convert.ToInt16((Width -
                          (sourceWidth * nPercent)) / 2);
        }
        else
        {
            nPercent = nPercentW;
            destY = System.Convert.ToInt16((Height -
                          (sourceHeight * nPercent)) / 2);
        }

        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);

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

        Graphics grPhoto = Graphics.FromImage(bmPhoto);
        grPhoto.Clear(Color.White);
        grPhoto.InterpolationMode =
                InterpolationMode.HighQualityBicubic;

        grPhoto.DrawImage(imgPhoto,
            new Rectangle(0, 0, destWidth, destHeight),
            new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
            GraphicsUnit.Pixel);

        grPhoto.Dispose();
        bmPhoto.Save(finalpath);
    }
Sachin
  • 152
  • 1
  • 17
1

From the documentation on Image.GetThumbnailImage method:

The GetThumbnailImage method works well when the requested thumbnail image has a size of about 120 x 120 pixels. If you request a large thumbnail image (for example, 300 x 300) from an Image that has an embedded thumbnail, there could be a noticeable loss of quality in the thumbnail image. It might be better to scale the main image (instead of scaling the embedded thumbnail) by calling the DrawImage method.

But your objective size is around 800x600 pixels, which is way above the indented use.

Thus, I recommend you to take a look at this answer about image resizing.

Community
  • 1
  • 1
Yurii
  • 4,811
  • 7
  • 32
  • 41