代码之家  ›  专栏  ›  技术社区  ›  darasd

调整jpeg图像的大小会影响其压缩吗?

  •  1
  • darasd  · 技术社区  · 16 年前

    我正在使用图形调整JPEG的大小。DrawImage方法(见下面的代码片段)。有人能确认这不会影响新图像的压缩吗? 我见过 this thread ,但我具体说的是JPEG的压缩。

        private byte[] getResizedImage(String url, int newWidth)
        {
            Bitmap bmpOut = null;
            System.IO.MemoryStream outStream = new System.IO.MemoryStream();
    
            //input image is disposable
            using (Bitmap inputImage = LoadImageFromURL(url))
            {
                ImageFormat format = inputImage.RawFormat;
                decimal ratio;  //ratio old width:new width
                int newHeight = 0;
    
                //*** If the image is smaller than a thumbnail just return it
                if (inputImage.Width < newWidth)
                    return null;
    
                ratio = (decimal)newWidth / inputImage.Width;
                decimal h = inputImage.Height * ratio;
                newHeight = (int)h;
    
                bmpOut = new Bitmap(newWidth, newHeight);
                Graphics g = Graphics.FromImage(bmpOut);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                // try testing with following options:
                //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                //g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                //g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
    
                g.FillRectangle(Brushes.White, 0, 0, newWidth, newHeight);
                g.DrawImage(inputImage, 0, 0, newWidth, newHeight);
                bmpOut.Save(outStream, getImageFormat(url));
            }
    
            return outStream.ToArray();
    
        }
    
    7 回复  |  直到 8 年前
        1
  •  6
  •   Zebra North    16 年前

    JPEG中没有保存“压缩”值。当你调整大小并保存它们时,它们会被压缩为你告诉保存函数使用的任何值。由于您没有传递值,它将只使用库的默认值。

        2
  •  4
  •   Ates Goral    16 年前

    调整图像大小会影响压缩,如果“压缩”是指可以在多少像素中嵌入多少细节。

    JPEG压缩算法倾向于模糊或均匀的图像,具有渐变和平坦的颜色块。因此,图像尺寸和结果文件大小之间没有直接相关性。最重要的是图像中的细节量(所有图像都保存为JPEG格式,损失率为80%):

    1. 原始的SO标志:

      Original http://magnetiq.com/exports/sologo/original.jpg

      File size: 4,483 bytes

    2. 保留尺寸并应用重高斯模糊:

      Blurred http://magnetiq.com/exports/sologo/blurred.jpg

      File size: 1,578 bytes

    3. 将原始大小调整为1/4:

      Shrunk http://magnetiq.com/exports/sologo/shrunk.jpg

      File size: 2,063 bytes

    请注意,模糊图像(2)虽然是缩小图像(3)的4倍大,但文件大小比(1)和(2)都小。模糊添加了很好的渐变,使JPEG压缩更愉快。

    小图像(3)虽然只有原始图像(1)的1/4像素,但并不是文件大小的1/4。这是因为缩小图像会产生更精细的细节,而JPEG压缩不喜欢这样。

        3
  •  3
  •   Apocalisp    16 年前

    当你调用“bmpOut.Save”时,你必须传递一些编码器参数来告诉方法你想用什么质量级别来保存它。

    http://msdn.microsoft.com/en-us/library/system.drawing.imaging.encoderparameters(VS.80).aspx

        4
  •  1
  •   Treb    16 年前

    不要将存储格式(JPEG)与您使用 Graphics 对象。存储格式被压缩 图像 对象处理解压缩的数据。

    这个 LoadImageFromURL 该方法解压缩存储在JPEG中的信息,您对解压缩的数据执行调整大小操作,当您将其保存到JPEG文件时,它会再次被压缩。

    由于JPEG压缩不是无损的,每次执行此过程时,图像质量都会降低: 解压、变异、再压缩 .

    如果我误解了你的问题:你在代码中没有设置用于保存图像的压缩因子,所以它将处于某个默认值。如果你想通过使用较小的压缩因子来减少质量损失,你需要明确地设置它。

        5
  •  0
  •   Diodeus - James MacFarlane    16 年前

    在你的代码中” HighQualityBicubic “是您正在使用的压缩。调整到较小的大小确实会删除细节,但这与压缩无关,它只与更少的像素有关。

        6
  •  0
  •   Adam Rosenfield    16 年前

    JPEG压缩是 lossy --并非所有原始数据都被准确存储。如果将图像压缩为JPEG并解压缩,则无法恢复到原始图像。每次压缩和解压缩时,你都会失去一些质量。

    对于一些简单的转换,如旋转JPEG,您可以使用以下程序无损地完成 jpegtran .

    但是,对于调整图像大小,您不能无损地进行。当你这样做的时候,你肯定会在一定程度上降低质量,尽管可能不会太大。如果可能的话,尝试使用原始无损图像作为调整大小操作的源,以获得尽可能高的质量,如果您连接了多个调整大小操作,则只需从原始图像进行一次调整大小。

        7
  •  0
  •   mangokun    16 年前

    与您已经找到的相同,但这是来自的VB.NET版本 Circumventing GDI+ default image compression .

    我个人建议JPEG质量设置为90L。

    推荐文章