反转捕获的指纹的颜色

时间:2014-11-10 13:08:35

标签: c# bitmap gdi fingerprint

我是图形编程的新手。我有一个代码如下从扫描仪捕获指纹:

    [DllImport("gdi32.dll")]
    static extern IntPtr CreateDIBitmap(IntPtr hdc, [In] ref BITMAPINFOHEADER lpbmih,
                                        uint fdwInit, byte[] lpbInit, byte[] lpbmi,
                                        uint fuUsage);


    /* constants for CreateDIBitmap */
    const int CBM_INIT = 0x04;   /* initialize bitmap */

    /* DIB color table identifiers */
    const int DIB_RGB_COLORS = 0; /* color table in RGBs */
    const int DIB_PAL_COLORS = 1; /* color table in palette indices */

    const int BI_RGB = 0;

    private Bitmap CreateBitmap(IntPtr hDC, Size bmpSize, byte[] data)
    {
        System.IO.MemoryStream mem = new System.IO.MemoryStream();
        System.IO.BinaryWriter bw = new System.IO.BinaryWriter(mem);
        //BITMAPINFO bmpInfo = new BITMAPINFO();
        BITMAPINFOHEADER bmiHeader = new BITMAPINFOHEADER();
        bmiHeader.biSize = 40;
        bmiHeader.biWidth = bmpSize.Width;
        bmiHeader.biHeight = bmpSize.Height;
        bmiHeader.biPlanes = 1;
        bmiHeader.biBitCount = 8;
        bmiHeader.biCompression = BI_RGB;
        bw.Write(bmiHeader.biSize);
        bw.Write(bmiHeader.biWidth);
        bw.Write(bmiHeader.biHeight);
        bw.Write(bmiHeader.biPlanes);
        bw.Write(bmiHeader.biBitCount);
        bw.Write(bmiHeader.biCompression);
        bw.Write(bmiHeader.biSizeImage);
        bw.Write(bmiHeader.biXPelsPerMeter);
        bw.Write(bmiHeader.biYPelsPerMeter);
        bw.Write(bmiHeader.biClrUsed);
        bw.Write(bmiHeader.biClrImportant);

        for (int i = 0; i < 256; i++)
        {
            bw.Write((byte)i);
            bw.Write((byte)i);
            bw.Write((byte)i);
            bw.Write((byte)0);
        }

        IntPtr hBitmap;
        if (data != null)
        {
            hBitmap = CreateDIBitmap(hDC, ref bmiHeader, CBM_INIT, data, mem.ToArray(), DIB_RGB_COLORS);
        }
        else
        {
            hBitmap = CreateDIBitmap(hDC, ref bmiHeader, 0, null, mem.ToArray(), DIB_RGB_COLORS);
        }
        return Bitmap.FromHbitmap(hBitmap);
    }

哪个生成带有Black背景和White脊的图像,但我想要反转这些颜色,我希望White背景带Black个脊。

有可能吗?

1 个答案:

答案 0 :(得分:1)

这是一个最简单的例子,但它很慢。它只是让你知道如何做到这一点。注意RGB(255,255,255)为白色,RGB(0,0,0)为黑色。如果您有像素黑色,那么这个想法很简单,所以从255减去每种颜色就会变成白色。反之亦然255 - 0 = 255。

public Bitmap InvertBitmapColor(Bitmap img)
{
  Bitmap result = new Bitmap(img);
  Color currentColor, newColor;
  for(int x=0; x < img.Width; x++)
    for(int y=0; y < img.Height; y++)
    {
       currentColor = img.GetPixel(x, y);
       newColor = Color.FromArgb(255 - currentColor.R, 255 - currentColor.G, 255 - currentColor.B);
       result.SetPixel(x, y, newColor);
    }
   return result;
}

如果您需要快速图像处理,请使用LockBits。您可以找到here

的示例