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

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

问题描述

我的工作,打印的图像文件,包括JPEG和TIFF的文件夹中的应用程序。的TIFF图像通常是黑色和白色(1bpp)。

加载图像之后,我想,以确定是否图像的颜色 B&放;是W 灰度这样我就可以发送图像到了正确的打印机(彩色打印机或黑白打印机)。

我使用了位图构造图像=新位图(文件名); 来加载图像<。 / p>

编辑:以检查像素深度答案是伟大的B&放大器; W--。对检查如果图像灰度无需经过的每一个像素进行迭代任何想法?


解决方案

要检查这个正确的方法是:

对于JPEG文件,你应该使用的 PropertyItems 收集位图。这可能包含相应的 EXIF​​ 标签,以帮助确定的位深度。下一步将是解析 JPEG头并查找帧开始标记,然后在图像中的组件数。

的最后一种方法是将JPEG加载到一个位图对象和具有forumla(宽×高×bytes_per_pixel)比较的像素的数量。所以,如果你实际加载的原始数据的字节位图和数量等于(宽*高),那么你知道它的图像具有每像素1字节一个安全的赌注,因此是灰色的。

您需要检查的最后一件事是位图本身的PixelFormat。

对于 TIFF 的文件格式,你也应该这样做用东西PropertyItems收集和检查在规格中提到相应的标记。如果这些失败,然后做形象字节比较,最后使用的PixelFormat属性作为最后的手段。

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

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

EDIT: 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?

解决方案

The proper way to check this is:

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.

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.

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

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天全站免登陆