2014-12-28 2 views
0

HBITMAP에서 특정 픽셀 RGB 값을 얻는 방법은 무엇입니까? StackOverflow에서 유사한 게시물을 읽으려고했지만 아무도 정말이 문제에 맞지 않습니다. 아래 코드는 HBITMAP에서 다른 위치 (원하는 위치가 아님)의 RGB 값을 얻는 것 같습니다. (user1118321의 용액)HBITMAP에서 특정 픽셀의 RGB 값 가져 오기

int width = rc.right - rc.left; 
    int height = rc.bottom - rc.top; 

    HDC hdcSource = hdc; // the source device context 
    HBITMAP hSource = hbmp; // the bitmap selected into the device context 

    BITMAPINFO MyBMInfo = {0}; 
    MyBMInfo.bmiHeader.biSize = sizeof(MyBMInfo.bmiHeader); 

    // Get the BITMAPINFO structure from the bitmap 
    if(0 == GetDIBits(hdcSource, hSource, 0, 0, NULL, &MyBMInfo, DIB_RGB_COLORS)) 
    { 
     // error handling 
    } 

    // create the pixel buffer 
    BYTE* Pixels = new BYTE[MyBMInfo.bmiHeader.biSizeImage]; 

    // We'll change the received BITMAPINFOHEADER to request the data in a 
    // 32 bit RGB format (and not upside-down) so that we can iterate over 
    // the pixels easily. 

    // requesting a 32 bit image means that no stride/padding will be necessary, 
    // although it always contains an (possibly unused) alpha channel 
    MyBMInfo.bmiHeader.biBitCount = 32; 
    MyBMInfo.bmiHeader.biCompression = BI_RGB; // no compression -> easier to use 
    // correct the bottom-up ordering of lines (abs is in cstdblib and stdlib.h) 
    MyBMInfo.bmiHeader.biHeight = abs(MyBMInfo.bmiHeader.biHeight); 

    // Call GetDIBits a second time, this time to (format and) store the actual 
    // bitmap data (the "pixels") in the buffer lpPixels 
    if(0 == GetDIBits(hdcSource, hSource, 0, MyBMInfo.bmiHeader.biHeight, 
         Pixels, &MyBMInfo, DIB_RGB_COLORS)) 
    { 
     // error handling 
    } 

    int PixelX = 221; 
    int PixelY = 14; 

    cout << "R: " << (int)Pixels[4*((PixelX-1)+(PixelY-1)*width)] << " | G: " << (int)Pixels[4*((PixelX-1)+(PixelY-1)*width)+1] << " | B: " << (int)Pixels[4*((PixelX-1)+(PixelY-1)*width)+2] << endl; 

편집

높이 1 Y는 0에서 시작하기 때문에하지 1

cout << "R: " << (int)Pixels[4*((PixelX)+(height-1-(PixelY))*width)] << " | G: " << (int)Pixels[4*((PixelX)+(height-1-(PixelY))*width)+1] << " | B: " << (int)Pixels[4*((PixelX)+(height-1-(PixelY))*width)+2] << endl; 
+1

['GetPixel'] (http://msdn.microsoft.com/en-us/library/dd144909.aspx)를 찾으십니까? –

+0

GetPixel은 매우 느려서 HBITMAP을 사용하여 모든 픽셀을 배열로 가져옵니다. –

+0

나는 혼란 스럽다. 원하는 "하나의 특정 픽셀"또는 "모든 픽셀"입니까? 당신의 주된 질문은 전자를 요구하고, 지금 당신은 후자를 요구하고있는 의견에 답합니다. 무엇 이니? –

답변

0

윈도우 비트 맵 Y 위쪽 증가 하부 왼쪽부터 그들의 화소를 저장할 . PixelY 대신 ((height - 1) - PixelY)을 사용해보고 도움이되는지 확인하십시오.

관련 문제