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

.NET Saving jpeg with the same quality as it was loaded(.NET 以与加载时相同的质量保存 jpeg)
本文介绍了.NET 以与加载时相同的质量保存 jpeg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大炮数码相机,我将它设置为以超精细质量拍照,它会输出一个 3 兆大小的 .jpg 文件.如果我在 ASP.NET 中像这样加载它(这对于更改它的 dpi 分辨率或裁剪它或其他程序很有用)

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)

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);

它输出一个大小只有 700KB 左右的 jpg.质量有所下降.

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

我也试过这样保存:

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

codecJpeg 在哪里

where codecJpeg is

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;
    }

和封装参数:

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.

我想问:

有没有办法在不硬编码质量值的情况下以与加载时相同的质量保存图像?

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

提前谢谢你

推荐答案

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

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?

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

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.)

我刚刚检查过,我下载的 jpeg 没有任何质量标签.

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

一种选择可能是计算文件最终应该有多大才能具有大致相同的质量,然后将其保存几次,进行二分搜索以找出最合适的质量.讨厌,但它可能会起作用.

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.

我暗中怀疑没有办法只保留原始质量设置,尽管我没有很好的怀疑理由......

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...

这篇关于.NET 以与加载时相同的质量保存 jpeg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)