2014-11-10 5 views
0

그래픽 프로그래밍을 처음 사용합니다. 나는 스캐너에서 지문 캡처 아래와 같은 코드를 가지고 :캡처 한 지문의 색을 반전하십시오.

[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 능선으로 이미지를 생성하지만 그 색상을 반전 할 내가 Black 능선과 White 배경을합니다.

가능합니까?

+0

어디에서 오는 데이터입니까? 데이터의 값을 반전시킬 수 있습니까? – ShayD

+0

예 가능합니다. 그것은 당신이 어떻게하고 싶은지에 달려 있습니다. 라이브러리를 사용할 수 있으며, 픽셀을 순환하여 색상을 변경할 수 있습니다. – Reniuz

+0

@ Renenuz 어느 것이 효율적입니까? 어떻게 할 수 있겠습니까? –

답변

1

(I thing) 가장 쉬운 방법이지만 느립니다. 어떻게하면 될지 생각할 수 있습니다. 참고 RGB(255,255,255)은 흰색이고 RGB(0,0,0)은 검정색입니다. 아이디어는 당신이 백색을 얻을 각 색깔을 뺀 255에서 화소 까만이있는 경우에 간단하다. 그리고 그 반대 255 - = 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 필요한 경우 0. 모두의 예를 찾을 수 있습니다 here

관련 문제