【问题标题】:Image quality loss while resizing image in C# for asp.net webform在 C# 中为 asp.net webform 调整图像大小时图像质量损失
【发布时间】:2012-10-14 00:50:43
【问题描述】:

我正在尝试调整图像大小并使用以下 sn-p 代码保存它。它工作正常,但一些图像在调整大小后会失去质量。当我检查时,原始图像看起来很好,只有调整大小的图像质量低。我不知道如何在调整大小的同时提高图像质量。

System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, MaxHeight, null, IntPtr.Zero);
// Clear handle to original file so that we can overwrite it if necessary
FullsizeImage.Dispose();
// Save resized picture
//NewImage.Save(NewFile);

if (fileExtension.ToLower() == ".jpg" || fileExtension.ToLower() == ".jpeg")
{
      NewImage.Save(NewFile, System.Drawing.Imaging.ImageFormat.Jpeg);
}

请帮帮我。谢谢。

【问题讨论】:

    标签: c# asp.net image-resizing


    【解决方案1】:

    你可以使用这个类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    
    /// <summary>
    /// Summary description for ResizeImage
    /// </summary>
    public class ResizeImage
    {
        public static Image Resize(Image imgToResize, int h, int w)
        {
            Size size = new Size(w, h);
    
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;
    
            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;
    
            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);
    
            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;
    
            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);
    
            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
    
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();
    
            return (Image)b;
        }
    }
    

    另外,您可以使用此代码来选择图像质量:

    graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphic.SmoothingMode = SmoothingMode.HighQuality; 
    graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
    graphic.CompositingQuality = CompositingQuality.HighQuality;
    

    点赞here

    【讨论】:

    • 你也应该DisposeBitmap。与显式调用 Dispose 不同,using 语句是首选的处理方式,因为它保证在出现 Exception 的情况下进行清理。
    • 啊。也许处理 Bitmap 并不是一个好主意,因为这就是返回的内容。此方法的用户应注意,返回的图像应在完成后处理。
    【解决方案2】:

    在调整图像大小时,您还应该记住几件事(经验法则,而不是福音,因为这取决于您在做什么等)...

    • 保持存储在 Db(或任何地方)中您可以获得的最大图像(并提供您的 Db/存储可以允许)。即您可以即时制作缩略图或缓存它们或其他东西,但最大的图像是“原始模型”之类的,
    • 缩小规模 - 如果可能 - 不要扩大规模,因为这永远不会那么好,
    • 保持图片的“比例”,
    • 小心图像处理,必须正确处理以避免添加噪音等。
    • 您使用的图像格式(用于保存或临时格式等)也非常重要,这也可能会破坏您的图像,因为不同的格式具有不同的算法并制作/牺牲图像的不同参数(无论是颜色或细节等),
    • 尽可能少地使用“转换” - 所以保留原始文件,对其进行简单的缩放 - 并尽可能保留在内存中,例如不要保存/加载等。

    希望这会有所帮助,

    【讨论】:

      猜你喜欢
      • 2017-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-17
      • 2013-10-18
      • 2011-04-18
      • 2011-01-20
      相关资源
      最近更新 更多