调整大小后的低质量图像

Posted

技术标签:

【中文标题】调整大小后的低质量图像【英文标题】:Low quality image after resizing 【发布时间】:2011-09-28 09:52:53 【问题描述】:

您好,我正在使用此功能来调整/上传图像的大小。在不改变全部功能的情况下,我可以进行任何快速调整以提高调整后的图像质量吗?

   FileUpload FileUpload1 =(FileUpload)ListView1.InsertItem.FindControl("FileUpload1");
                string virtualFolder = "~/albume/";
                string physicalFolder = Server.MapPath(virtualFolder);
                string fileName = Guid.NewGuid().ToString();
                string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
                FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension));
                //test resize
                System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/albume/") + fileName + extension);
                int srcWidth = img.Width;
                int srcHeight = img.Height;
                int thumbHeight = (int)((800.0 / srcWidth) * srcHeight);
                System.Drawing.Image thumb = img.GetThumbnailImage(800, thumbHeight, null, IntPtr.Zero);
                img.Dispose();
                FileUpload1.Dispose();
                thumb.Save(Server.MapPath("~/albume/") + fileName + extension, System.Drawing.Imaging.ImageFormat.Jpeg);

                //end resize
                myAlbum.poza = fileName + extension;

【问题讨论】:

您的图片是否包含缩略图?如果是这样,这可能适用:“如果您从具有嵌入式缩略图的图像中请求较大的缩略图图像(例如,300 x 300),则缩略图图像的质量可能会明显下降。最好是通过调用 DrawImage 方法来缩放主图像(而不是缩放嵌入的缩略图)。”来自the documentation。 Resizing an Image without losing any quality的可能重复 High Quality Image Scaling C# 的可能副本 正确调整图像大小is difficult, and has many pitfalls。支持 jpeg 的正确实现需要 5 页代码。支持 GIF 的正确实现需要 80 页代码。我建议using a library to handle the resizing, cropping, and encoding properly。 【参考方案1】:

代替这段代码:

thumb.Save(Server.MapPath("~/albume/") + fileName + extension, System.Drawing.Imaging.ImageFormat.Jpeg);

使用这个:

System.Drawing.Imaging.ImageCodecInfo[] info = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
System.Drawing.Imaging.EncoderParameters param = new System.Drawing.Imaging.EncoderParameters(1);
param.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
thumb.Save(Server.MapPath("~/albume/") + fileName + extension, info[1], param);

【讨论】:

【参考方案2】:

使用这个 sn-p:

Bitmap newImage = new Bitmap(newWidth, newHeight);
using (Graphics gr = Graphics.FromImage(newImage))

    gr.SmoothingMode = SmoothingMode.AntiAlias;
    gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
    gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
    gr.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));

从这个问题/答案中窃取:Resizing an Image without losing any quality 查看 Kris 的超投票答案并投票:)

【讨论】:

谢谢我正在尝试实现您推荐的功能,但我得到 System.Runtime.InteropServices.ExternalException: GDI+ 中发生一般错误。 .. 处理这个我是 C# 的新手【参考方案3】:

在我的网站上,我使用以下代码在将用户图像发送到页面之前调整其大小。

您也许可以使用类似的东西 - 尝试研究各种插值模式。

using (Graphics graphics = Graphics.FromImage(target))

    graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

    graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
    graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
    graphics.Clear(ColorTranslator.Fromhtml("#F4F6F5"));
    graphics.DrawImage(bmp, 0, 0, 145, 145);
    using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
    
        target.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);                    
        memoryStream.WriteTo(context.Response.OutputStream);
    

【讨论】:

【参考方案4】:

最好不要使用“GetThumbnailImage”这样会降低质量。根据http://msdn.microsoft.com/de-de/library/system.drawing.image.getthumbnailimage%28VS.80%29.aspx

通过重绘来调整图像大小:

private static Image resizeImage(Image imgToResize, Size size)

   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;

found on

【讨论】:

以上是关于调整大小后的低质量图像的主要内容,如果未能解决你的问题,请参考以下文章

使用gd在php中调整图像大小后的黑色背景

调整大小后的图像质量非常低——Java

PHP图像动态调整大小与存储调整大小的图像

图像调整大小和裁剪

Ionic2 Signature Pad 调整图像大小

如何查看精确调整大小的图像放入 Vision 框架?