Search code examples
c#bitmapgdifingerprint

Invert the colors of Captured finger print


I am new to graphic programming. I have a code as below to capture the finger print from the scanner:

    [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);
    }

Which generates an image with Black background and White ridges but I want to invert those color and I want White background with Black ridges.

Is it possible ?


Solution

  • Here is an example which is (I thing) the easiest but it is slow. It's just to give you an idea how it can be done. Note RGB(255,255,255) is white and RGB(0,0,0) is black color. The idea is simple if you have pixel black so from 255 subtracting each color you will get white. And vice versa 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;
    }
    

    If you need fast image processing use LockBits. You can find example of both here