2012-06-22 39 views
0

我可以使用GetDIBits加載當前窗口的顏色內容,但我不知道如何從一個位置加載圖像的顏色。有人能告訴我該怎麼做嗎?使用GetDIBits訪問圖像的像素顏色

 char str[256]; 
     HDC hdc; 
     HWND hDesktopWnd; HDC hDesktopDC; HDC hCaptureDC; 
     HBITMAP hCaptureBitmap; BITMAPINFO bmi = {0}; 
     RGBQUAD *pPixels; 
     int nScreenWidth, nScreenHeight; 
     hdc = GetDC(hwnd); 
     GetWindowRect(hwnd,&rect); 

     hdc = GetDC(hwnd); 
     if(GetWindowRect(hwnd, &rect)) 
     { 
     width = rect.right - rect.left; 
     height = rect.bottom - rect.top; 
     } 

      nScreenWidth = GetSystemMetrics(SM_CXSCREEN); 
      nScreenHeight = GetSystemMetrics(SM_CYSCREEN); 
      hDesktopWnd = GetDesktopWindow(); 
      hDesktopDC = GetDC(hwnd); 
      hCaptureDC = CreateCompatibleDC(hDesktopDC); 
      hCaptureBitmap = CreateCompatibleBitmap(hDesktopDC, nScreenWidth, nScreenHeight); 
      SelectObject(hCaptureDC, hCaptureBitmap); 
      BitBlt(hCaptureDC, 0, 0, nScreenWidth, nScreenHeight, hDesktopDC, 0,0, SRCCOPY|CAPTUREBLT); 

      bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader); 
      bmi.bmiHeader.biWidth = nScreenWidth; 
      bmi.bmiHeader.biHeight = nScreenHeight; 
      bmi.bmiHeader.biPlanes = 1; 
      bmi.bmiHeader.biBitCount = 32; 
      bmi.bmiHeader.biCompression = BI_RGB; 

      pPixels = new RGBQUAD[nScreenWidth * nScreenHeight]; 

      ::GetDIBits(hCaptureDC, 
         hCaptureBitmap, 
         0, 
         nScreenHeight, 
         pPixels, 
         &bmi, 
         DIB_RGB_COLORS); 

我這樣

  for(int i= 0;i< nScreenHeight; i++){ 
      for(int j= 0;j< nScreenWidth; j++){ 
       col.red_palette[i][j] = pPixels[(nScreenWidth * (nScreenHeight-(i+1))) + j].rgbRed; 
       col.green_palette[i][j] = pPixels[(nScreenWidth * (nScreenHeight-(i+1))) + j].rgbGreen; 
       col.blue_palette[i][j] = pPixels[(nScreenWidth * (nScreenHeight-(i+1))) + j].rgbBlue; 
      } 
      } 


      delete [] pPixels; 
      ReleaseDC(hDesktopWnd, hDesktopDC); 
      DeleteDC(hCaptureDC); 
      DeleteObject(hCaptureBitmap); 

我是新加載的顏色到一個數組中的程序窗口,只是想知道如何將圖像加載到HDC。

正如雷蒙曾建議我已經過了第二個參數

HBITMAP hCaptureBitmap; 
hCaptureBitmap = (HBITMAP)LoadImage(NULL, szFileName, IMAGE_BITMAP, 0, 0, 
      LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE); 

我仍然只捕獲活動窗口的顏色,而不是圖像的。我如何更改設備手柄來反映這一點。

HWND hDesktopWnd; HDC hDesktopDC; HDC hCaptureDC; 
hDesktopWnd = GetDesktopWindow(); 
hDesktopDC = GetDC(hwnd); 
hCaptureDC = CreateCompatibleDC(hDesktopDC); 

回答

1
RGBQUAD rgba = pPixels[y * nScreenWidth + x]; 

注意,Y = 0是在圖像的底部,而不是頂部,你可能期望。

+0

是的,我更新了代碼,以顯示我所做的。 –

+0

@GambitKing,如果這不是你想知道的,那麼我不知道你在問什麼。你不是很清楚。 –

+0

對不起,上面的代碼將當前窗口的圖像顏色加載到數組上,我想打開一個特定圖像的顏色到數組上,我想知道該怎麼做? –