如何检查位图的颜色深度? [英] How can I check the color depth of a Bitmap?

查看:22
本文介绍了如何检查位图的颜色深度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,用于打印图像文件的文件夹,包括 JPEG 和 TIFF.TIFF 图像通常是黑白 (1bpp).

I'm working on an application that prints a folder of image files, including JPEG and TIFF. The TIFF images are usually Black and White (1bpp).

加载图像后,我想确定图像是彩色还是黑白灰度,以便我可以发送图像到正确的打印机(彩色打印机或黑白打印机).

After loading the image, I want to determine if the image is Color or B&W or Grayscale so I can send the image to the right printer (color printer or black and white printer).

我正在使用 Bitmap 构造函数 image = new Bitmap(filename); 来加载图像.

I'm using the Bitmap constructor image = new Bitmap(filename); to load the image.

检查像素深度的答案非常适合黑白.关于检查图像是否为灰度而不遍历每个像素的任何想法?

The answer to check the pixel depth is great for B&W. Any ideas on checking if the image is grayscale without iterating through every pixel?

推荐答案

正确的检查方法是:

对于 JPEG 文件,您应该使用 PropertyItems 位图的集合.这可能包含适当的 EXIF 标签以帮助确定位深度.下一步是解析 JPEG 标头 并寻找帧的开始"' 标记,然后是图像中的组件数.

For JPEG files you should check the appropriate properties using the PropertyItems collection of the Bitmap. This may contain the appropriate EXIF tags to help determine the bit depth. The next step would be to parse the JPEG header and look for the 'start of frame' marker and then the number of components in the image.

最后一种方法是将JPEG加载到Bitmap对象中,并与forumla(width * height * bytes_per_pixel)比较像素数.因此,如果您加载位图并且实际原始数据的字节数等于(宽度 * 高度),那么您就知道图像每个像素有 1 个字节是一个安全的赌注,因此是灰度.

The final method is to load the JPEG into a Bitmap object and compare the number of pixels with the forumla (width * height * bytes_per_pixel). So if you load the bitmap and number of bytes of actual raw data is equal to (width * height) then you know it's a safe bet that the image has 1 byte per pixel and as such is grayscale.

您最后需要检查的是位图本身的 PixelFormat.

The last thing you'll want to check is the PixelFormat of the bitmap itself.

对于 TIFF 文件格式,您应该执行同样的事情使用 PropertyItems 集合并检查规范中提到的适当标签.如果这些失败,则进行图像字节比较,最后使用 PixelFormat 属性作为最后的手段.

For the TIFF file format you should do the same thing using the PropertyItems collection and check the appropriate tag mentioned in the spec. If these fail then do the image byte comparison and finally use the PixelFormat property as a last resort.

这篇关于如何检查位图的颜色深度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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