调整图像大小会损坏像素

时间:2021-12-01 09:01:03

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.

如果图像大于800 X 600像素,我的要求是调整图像大小。我必须根据纵横比将其调整为800 X 600分辨率。

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:

请参阅下面我上传到服务器的原始图像:

调整图像大小会损坏像素

After re-sizing it, it is damages like below 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.

希望你有我的问题。调整大小的图像在像素中受到损坏。请让我知道我错在哪里或问题出在哪里。

2 个解决方案

#1


2  

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

#2


1  

From the documentation on Image.GetThumbnailImage method:

从Image.GetThumbnailImage方法的文档:

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.

当请求的缩略图图像的大小约为120 x 120像素时,GetThumbnailImage方法很有效。如果从具有嵌入式缩略图的图像请求大型缩略图图像(例如,300 x 300),则缩略图图像中可能会出现明显的质量损失。通过调用DrawImage方法缩放主图像(而不是缩放嵌入的缩略图)可能更好。

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

但是你的目标大小约为800x600像素,远高于缩进使用。

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

因此,我建议您看看有关图像大小调整的答案。

#1


2  

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

#2


1  

From the documentation on Image.GetThumbnailImage method:

从Image.GetThumbnailImage方法的文档:

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.

当请求的缩略图图像的大小约为120 x 120像素时,GetThumbnailImage方法很有效。如果从具有嵌入式缩略图的图像请求大型缩略图图像(例如,300 x 300),则缩略图图像中可能会出现明显的质量损失。通过调用DrawImage方法缩放主图像(而不是缩放嵌入的缩略图)可能更好。

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

但是你的目标大小约为800x600像素,远高于缩进使用。

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

因此,我建议您看看有关图像大小调整的答案。