检查图像是否有颜色

时间:2012-04-26 12:55:45

标签: c# .net image gdi+ pixelformat

我正在试图弄清楚图像是否有颜色。在this StackOverflow问题上,有回复说我应该检查PixelFormat的{​​{1}}枚举。不幸的是,回复对我来说不是很清楚。是否可以安全地检查Image是否与image.PixelFormat不同,以确定它是彩色图像?枚举的其他值怎么样? MSDN文档不是很清楚......

3 个答案:

答案 0 :(得分:13)

你可以通过避免使用Color.FromArgb并迭代字节而不是整数来改进这一点,但我认为这对你来说更具可读性,并且更容易理解为一种方法。

一般的想法是将图像绘制成已知格式的位图(32bpp ARGB), 然后检查该位图是否包含任何颜色。

锁定位图的位允许您使用不安全的代码比使用GetPixel快几倍地迭代它的颜色数据。

如果像素的alpha为0,那么它显然是GrayScale,因为alpha 0意味着它完全不透明。除此之外 - 如果R = G = B,则它是灰色的(如果它们= 255,则为黑色)。

private static unsafe bool IsGrayScale(Image image)
{
    using (var bmp = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb))
    {
        using (var g = Graphics.FromImage(bmp))
        {
            g.DrawImage(image, 0, 0);
        }

        var data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);

        var pt = (int*)data.Scan0;
        var res = true;

        for (var i = 0; i < data.Height * data.Width; i++)
        {
            var color = Color.FromArgb(pt[i]);

            if (color.A != 0 && (color.R != color.G || color.G != color.B))
            {
                res = false;
                break;
            }
        }

        bmp.UnlockBits(data);

        return res;
    }
}

答案 1 :(得分:0)

SimpleVar's answer is mostly correct: that code doesn't properly handle when the source image has an indexed color format.

To solve this, simply replace the outer using block with:

using (var bmp = new Bitmap(image)) {

and remove the inner using entirely, as the Graphics object is no longer needed. This will create a perfect copy of the image in a non-indexed format, regardless of the original image's pixel format.

答案 2 :(得分:-1)

    private bool isGrayScale(Bitmap processedBitmap)
    { 
        bool res = true;
        unsafe
        {
            System.Drawing.Imaging.BitmapData bitmapData = processedBitmap.LockBits(new Rectangle(0, 0, processedBitmap.Width, processedBitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, processedBitmap.PixelFormat);
            int bytesPerPixel = System.Drawing.Bitmap.GetPixelFormatSize(processedBitmap.PixelFormat) / 8;
            int heightInPixels = bitmapData.Height;
            int widthInBytes = bitmapData.Width * bytesPerPixel;
            byte* PtrFirstPixel = (byte*)bitmapData.Scan0;
            Parallel.For(0, heightInPixels, y =>
            {
                byte* currentLine = PtrFirstPixel + (y * bitmapData.Stride);
                for (int x = 0; x < widthInBytes; x = x + bytesPerPixel)
                {
                    int b = currentLine[x];
                    int g = currentLine[x + 1];
                    int r = currentLine[x + 2];
                    if (b != g || r != g)
                    {
                        res = false;
                        break;
                    }
                }
            });
            processedBitmap.UnlockBits(bitmapData);
        }
        return res;
    }