不Image.Save()使用什么样的品质级别JPEG文件? [英] What quality level does Image.Save() use for jpeg files?

查看:1130
本文介绍了不Image.Save()使用什么样的品质级别JPEG文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚得到一个真正的惊喜,当我装一个JPG文件,转过身来,并与100质量保存它和大小几乎4倍原件。为了进一步调查我打开并保存没有明确设置质量和文件大小是完全一​​样的。我想这是因为什么都没有改变,所以它只是写完全相同的位回文件。为了检验这一假设,我画了一个大胖子线斜对面的形象和不设置质量又节省(这时候我预期的文件跳起来,因为这将是脏),但它下降〜10KB!

在这一点上我真的不明白发生了什么时,我只需调用Image.Save()瓦特/出指定COM pression质量。如何在文件大小如此接近(图像被修改后)到原来的大小时,没有质量设置然而,当我将质量设置为100(基本上没有COM pression)文件的大小是数倍于原来的大?

我读过关于Image.Save的文档()和它的缺乏了解正在发生什么幕后的任何细节。我GOOGLE了我能想到的各种方法,但我找不到可以解释我所看到的任何其他信息。我已经工作了31个小时了,也许我失去了一些东西明显; 0)

这一切,来得虽然我实施一些库方法将图像保存到数据库中。我已经超负荷我们的SaveImage方法,以允许明确设置了质量,我的测试过程中我遇到了奇怪的(对我)的结果如上所述。你可以摆脱任何光线将AP preciated。

下面是一些code,将说明什么我遇到:

 字符串文件名= @C:\\ TEMP \\图像测试\\ hh.jpg
字符串destPath = @C:\\ TEMP \\图像测试\\;使用(形象画像= Image.FromFile(文件名))
{
    图像codecInfo codecInfo = ImageUtils.GetEn coderInfo(ImageFormat.Jpeg);    //设置质量
    恩coderParameters参数=新恩coderParameters(1);    //品质:10
    parameters.Param [0] =新恩coderParameter(
        System.Drawing.Imaging.En coder.Quality,10L);
    image.Save(destPath +10.JPG,codecInfo,参数);    //品质:75
    parameters.Param [0] =新恩coderParameter(
        System.Drawing.Imaging.En coder.Quality,75L);
    image.Save(destPath +75.jpg,codecInfo,参数);    //质量:100
    parameters.Param [0] =新恩coderParameter(
        System.Drawing.Imaging.En coder.Quality,100L);
    image.Save(destPath +100.jpg,codecInfo,参数);    //默认
    image.Save(destPath +default.jpg,ImageFormat.Jpeg);    //整个图像大线
    使用(图形G = Graphics.FromImage(图片))
    {
        使用(笔笔=新朋(Color.Red,50F))
        {
            g.DrawLine(笔,0,0,image.Width,image.Height);
        }
    }    image.Save(destPath +的大红色line.jpg,ImageFormat.Jpeg);
}公共静态图像codecInfo GetEn coderInfo(的imageformat格式)
{
    返回图像codecInfo.GetImageEn codeRS()了ToList()。找到(委托(图片codecInfo codeC)
    {
        返回codec.FormatID == format.Guid;
    });
}


解决方案

使用反射器,事实证明 Image.Save()归结为GDI +功能的 GdipSaveImageToFile ,用连接coderParams NULL。所以我觉得这个问题是,当它得到一个空的JPEG EN codeR做什么连接coderParams 。 75%已经在这里建议,但我无法找到任何可靠的参考。

修改您也许可以找到自己运行您的程序上面的1..100质量值,并将其与保存在默认画质的JPG比较(使用,比如说,FC.EXE / B)

I just got a real surprise when I loaded a jpg file and turned around and saved it with a quality of 100 and the size was almost 4x the original. To further investigate I open and saved without explicitly setting the quality and the file size was exactly the same. I figured this was because nothing changed so it's just writing the exact same bits back to a file. To test this assumption I drew a big fat line diagonally across the image and saved again without setting quality (this time I expected the file to jump up because it would be "dirty") but it decreased ~10Kb!

At this point I really don't understand what is happening when I simply call Image.Save() w/out specifying a compression quality. How is the file size so close (after the image is modified) to the original size when no quality is set yet when I set quality to 100 (basically no compression) the file size is several times larger than the original?

I've read the documentation on Image.Save() and it's lacking any detail about what is happening behind the scenes. I've googled every which way I can think of but I can't find any additional information that would explain what I'm seeing. I have been working for 31 hours straight so maybe I'm missing something obvious ;0)

All of this has come about while I implement some library methods to save images to a database. I've overloaded our "SaveImage" method to allow explicitly setting a quality and during my testing I came across the odd (to me) results explained above. Any light you can shed will be appreciated.

Here is some code that will illustrate what I'm experiencing:

string filename = @"C:\temp\image testing\hh.jpg";
string destPath = @"C:\temp\image testing\";

using(Image image = Image.FromFile(filename))
{
    ImageCodecInfo codecInfo = ImageUtils.GetEncoderInfo(ImageFormat.Jpeg);

    //  Set the quality
    EncoderParameters parameters = new EncoderParameters(1);

    // Quality: 10
    parameters.Param[0] = new EncoderParameter(
        System.Drawing.Imaging.Encoder.Quality, 10L);
    image.Save(destPath + "10.jpg", codecInfo, parameters);

    // Quality: 75
    parameters.Param[0] = new EncoderParameter(
        System.Drawing.Imaging.Encoder.Quality, 75L);
    image.Save(destPath + "75.jpg", codecInfo, parameters);

    // Quality: 100
    parameters.Param[0] = new EncoderParameter(
        System.Drawing.Imaging.Encoder.Quality, 100L);
    image.Save(destPath + "100.jpg", codecInfo, parameters);

    //  default
    image.Save(destPath + "default.jpg", ImageFormat.Jpeg);

    //  Big line across image
    using (Graphics g = Graphics.FromImage(image))
    {
        using(Pen pen = new Pen(Color.Red, 50F))
        {
            g.DrawLine(pen, 0, 0, image.Width, image.Height);
        }
    }

    image.Save(destPath + "big red line.jpg", ImageFormat.Jpeg);
}

public static ImageCodecInfo GetEncoderInfo(ImageFormat format)
{
    return ImageCodecInfo.GetImageEncoders().ToList().Find(delegate(ImageCodecInfo codec)
    {
        return codec.FormatID == format.Guid;
    });
}

解决方案

Using reflector, it turns out Image.Save() boils down to the GDI+ function GdipSaveImageToFile, with the encoderParams NULL. So I think the question is what the JPEG encoder does when it gets a null encoderParams. 75% has been suggested here, but I can't find any solid reference.

EDIT You could probably find out for yourself by running your program above for quality values of 1..100 and comparing them with the jpg saved with the default quality (using, say, fc.exe /B)

这篇关于不Image.Save()使用什么样的品质级别JPEG文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆