.NET使用与加载时相同的质量保存jpeg

时间:2023-01-21 21:18:48

I have a cannon digital camera and I set it to take pictures with superfine quality and it outputs a .jpg file 3 mega in size. If I load it like this in ASP.NET(this is useful to change it's dpi resolution or crop it or whaterver)

我有一个大炮数码相机,我把它设置为拍摄超细质量的照片,它输出一个3兆的.jpg文件。如果我在ASP.NET中加载它(这有助于更改它的dpi分辨率或裁剪它或whaterver)

imgPicture = Image.FromFile(Config.WorkDirectory + this.TempPhotoName);
bmpPicture = new Bitmap(imgPicture);

and then I save it again like this:

然后我再次保存它:

bmpModified.Save(Config.WorkDirectory + this.TempPhotoName,System.Drawing.Imaging.ImageFormat.Jpeg);

it outputs a jpg that is only 700KB or so in size. There is a loss of quality.

它输出的jpg大小只有700KB左右。质量下降。

I also tried saving it like this:

我也试过像这样保存它:

bmpPicture.Save(Config.WorkDirectory + this.TempPhotoName, codecJpeg, encparams);

where codecJpeg is

codecJpeg是哪里的

ImageCodecInfo codecJpeg = this.getEncoderInfo("image/jpeg");


    private ImageCodecInfo getEncoderInfo(string mimeType)
    {
        // Get image codecs for all image formats
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

        // Find the correct image codec
        for (int i = 0; i < codecs.Length; i++)
            if (codecs[i].MimeType == mimeType)
                return codecs[i];
        return null;
    }

and encparams:

EncoderParameters encparams = new EncoderParameters(1);
encparams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 97L);

This way the size(and I suspect also the quality) is maintained but I am inputing the quality by hand.

这样,尺寸(我怀疑质量)也保持不变,但我手工输入质量。

I want to ask:

我想问一下:

Is there a way to save the image with the same quality as it was loaded without hardcoding the quality value?

有没有一种方法可以保存图像与加载时的质量相同,而无需硬编码质量值?

Thank you in advance

先感谢您

3 个解决方案

#1


So it sounds like you know how to set the quality, you really just need to know how to fetch the quality from the original image?

所以听起来你知道如何设置质量,你真的只需要知道如何从原始图像中获取质量?

I suspect that Image.PropertyItems is your friend, if the quality is in the metadata to start with. (I don't know if there's even a standard scale for quality within JPEG encoders.)

我怀疑Image.PropertyItems是你的朋友,如果质量在元数据中开始。 (我不知道JPEG编码器中是否还有质量标准。)

EDIT: I've just checked, and a jpeg I downloaded didn't have any tags for quality.

编辑:我刚刚检查过,我下载的jpeg没有任何质量标签。

One option might be to work out how big the file should end up to have roughly the same quality, and then save it several times, doing a binary search to work out the most appropriate quality. Icky, but it might just work.

一种选择可能是找出文件最终应该具有大致相同质量的大小,然后保存几次,进行二分查找以找出最合适的质量。 Icky,但它可能会奏效。

I have a sneaking suspicion that there isn't a way to just preserve the original quality setting, although I don't have very good grounds for that suspicion...

我有一种潜在的怀疑,即没有办法保持原有的质量设置,尽管我没有很好的理由怀疑......

#2


You must use the additional parameters which tell GDI+ to detect the color scheme.

您必须使用告知GDI +的其他参数来检测颜色方案。

For instance:

using (var img = Image.FromStream(fileupload.InputStream, true,true))
{
    img.Save(fileOnDisk, ImageFormat.Jpeg);
}

using (var img = Image.FromFile("yourimage", true))
{
    img.Save(fileOnDisk, ImageFormat.Jpeg);
}

#3


Read here how to save image without re-encoding image: How-to: Re-encode a JPEG Image with Metadata.

在此处阅读如何在不重新编码图像的情况下保存图像:操作方法:使用元数据重新编码JPEG图像。

However, if you do cropping or another image manipulation it impossible to do it without quality loss (well technically it is possible to do loss-less crop, if you work with boundaries that multiply of 16, but AFAIK it is cannot be done with libraries available in .net).

但是,如果你进行裁剪或其他图像处理,就不可能在没有质量损失的情况下进行操作(从技术上讲,如果使用16倍的边界,可以进行无损裁剪,但AFAIK不能用库来完成可在.net获得。

#1


So it sounds like you know how to set the quality, you really just need to know how to fetch the quality from the original image?

所以听起来你知道如何设置质量,你真的只需要知道如何从原始图像中获取质量?

I suspect that Image.PropertyItems is your friend, if the quality is in the metadata to start with. (I don't know if there's even a standard scale for quality within JPEG encoders.)

我怀疑Image.PropertyItems是你的朋友,如果质量在元数据中开始。 (我不知道JPEG编码器中是否还有质量标准。)

EDIT: I've just checked, and a jpeg I downloaded didn't have any tags for quality.

编辑:我刚刚检查过,我下载的jpeg没有任何质量标签。

One option might be to work out how big the file should end up to have roughly the same quality, and then save it several times, doing a binary search to work out the most appropriate quality. Icky, but it might just work.

一种选择可能是找出文件最终应该具有大致相同质量的大小,然后保存几次,进行二分查找以找出最合适的质量。 Icky,但它可能会奏效。

I have a sneaking suspicion that there isn't a way to just preserve the original quality setting, although I don't have very good grounds for that suspicion...

我有一种潜在的怀疑,即没有办法保持原有的质量设置,尽管我没有很好的理由怀疑......

#2


You must use the additional parameters which tell GDI+ to detect the color scheme.

您必须使用告知GDI +的其他参数来检测颜色方案。

For instance:

using (var img = Image.FromStream(fileupload.InputStream, true,true))
{
    img.Save(fileOnDisk, ImageFormat.Jpeg);
}

using (var img = Image.FromFile("yourimage", true))
{
    img.Save(fileOnDisk, ImageFormat.Jpeg);
}

#3


Read here how to save image without re-encoding image: How-to: Re-encode a JPEG Image with Metadata.

在此处阅读如何在不重新编码图像的情况下保存图像:操作方法:使用元数据重新编码JPEG图像。

However, if you do cropping or another image manipulation it impossible to do it without quality loss (well technically it is possible to do loss-less crop, if you work with boundaries that multiply of 16, but AFAIK it is cannot be done with libraries available in .net).

但是,如果你进行裁剪或其他图像处理,就不可能在没有质量损失的情况下进行操作(从技术上讲,如果使用16倍的边界,可以进行无损裁剪,但AFAIK不能用库来完成可在.net获得。