用较低的位深度(例如24至16)保存jpg(C#) [英] save jpg with lower bit depth (24 to 16 for instance) (C#)

查看:569
本文介绍了用较低的位深度(例如24至16)保存jpg(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将24位和32位jpeg和png文件转换为较低的位深度(16).我找到了一些执行此操作的代码,但是尽管文件大小较小,但生成的图像仍保留原始文件的位深.

I need to convert 24- and 32-bits jpeg and png-files to a lower bit depth (16). I found some code to do this, but the resulting images keep the bit depth of the original file although there file size is lower.

Image img = Image.FromFile(filePathOriginal);
Bitmap bmp = ConvertTo16bpp(img);
EncoderParameters parameters = new EncoderParameters();
parameters.Param[0] = new EncoderParameter(Encoder.ColorDepth, 16);
bmp.Save(filePathNew, jpgCodec, parameters);
bmp.Dispose();
img.Dispose();

...

private static Bitmap ConvertTo16bpp(Image img) {
    var bmp = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
    using (var gr = Graphics.FromImage(bmp))
    {
         gr.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height));
    }

    return bmp;
}

有什么想法吗?

谢谢, 弗兰克

推荐答案

JPEG是一种三色格式.它通常每种颜色有8位,但是可以有12位或16位.因此24 = 3x8位的颜色是合理的,但是16位或32位根本不可能.它只是不除以三. 3x16 = 48是可能的,但这是更高的颜色深度. JPEG是专为照片设计的,因此支持低于3x8的位深度是没有意义的.没有任何好处.

JPEG is a three-color format. It usually has 8 bits per color, but can have 12 or 16. 24=3x8 bits of color is therefore reasonable, but 16 or 32 is simply impossible. It just doesn't divide by three. 3x16=48 would be possible, but that's a higher color depth. JPEG is designed for photo's, and it doesn't make sense to support lower bit depths than 3x8. There's no benefit in that.

现在,代码中的16位图像是什么?这是仅使用65535种颜色的原始内存近似值.当您将其保存回去时,将获得24位JPEG.显然,您的JPEG编码器不知道如何创建48位JPEG.即使这样做,由于内存中的图像仍然只有65536种颜色,因此会浪费一些比特.

Now, what is the 16 bit image in your code? It's an imprecise in-memory approximation of the original, using only 65535 colors. When you save that back, you get a 24 bits JPEG. Apparently your JPEG encoder doesn't know how to create an 48 bits JPEG. Even if it did, it would be a waste of bits since the in-memory image only has 65536 colors anyway.

总结:问题出在任务上.没有像65536色JPEG这样的东西.

To summarize: what is going wrong is the task. There's no such thing as a 65536 color JPEG.

这篇关于用较低的位深度(例如24至16)保存jpg(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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