2012-01-25 14 views
5

Ho cercato e ho capito che dovrò usare GetDIBits(). Non so cosa fare con il parametro LPVOID lpvBits out.Come accedere al colore dei pixel all'interno di una bitmap?

Qualcuno potrebbe spiegarmelo? Ho bisogno di ottenere le informazioni sul colore dei pixel in una forma a matrice bidimensionale in modo che possa recuperare le informazioni per una particolare coppia di coordinate (x, y).

Sto programmando in C++ utilizzando l'API Win32.

+0

possibile duplicato del [GetDIBits e loop-through pixel usando X, Y] (h ttp: //stackoverflow.com/questions/3688409/getdibits-and-loop-through-pixels-using-x-y) –

risposta

1

io non sono sicuro se questo è quello che stai cercando, ma GetPixel fa più o meno quello che ti serve ... almeno da quello che posso dire dalla descrizione della funzione

+1

Generalmente non si desidera utilizzare GetPixel per estrarre tutti i pixel, ma diventa piuttosto lento. – pezcode

5

prima hai bisogno di una bitmap e aperto si

HBITMAP hBmp = (HBITMAP) LoadImage(GetModuleHandle(NULL), _T("test.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); 

if(!hBmp) // failed to load bitmap 
    return false; 

//getting the size of the picture 
BITMAP bm; 
GetObject(hBmp, sizeof(bm), &bm); 
int width(bm.bmWidth), 
    height(bm.bmHeight); 

//creating a bitmapheader for getting the dibits 
BITMAPINFOHEADER bminfoheader; 
::ZeroMemory(&bminfoheader, sizeof(BITMAPINFOHEADER)); 
bminfoheader.biSize  = sizeof(BITMAPINFOHEADER); 
bminfoheader.biWidth  = width; 
bminfoheader.biHeight  = -height; 
bminfoheader.biPlanes  = 1; 
bminfoheader.biBitCount = 32; 
bminfoheader.biCompression = BI_RGB; 

bminfoheader.biSizeImage = width * 4 * height; 
bminfoheader.biClrUsed = 0; 
bminfoheader.biClrImportant = 0; 

//create a buffer and let the GetDIBits fill in the buffer 
unsigned char* pPixels = new unsigned char[(width * 4 * height)]; 
if(!GetDIBits(CreateCompatibleDC(0), hBmp, 0, height, pPixels, (BITMAPINFO*) &bminfoheader, DIB_RGB_COLORS)) // load pixel info 
{ 
    //return if fails but first delete the resources 
    DeleteObject(hBmp); 
    delete [] pPixels; // delete the array of objects 

    return false; 
} 

int x, y; // fill the x and y coordinate 

unsigned char r = pPixels[(width*y+x) * 4 + 2]; 
unsigned char g = pPixels[(width*y+x) * 4 + 1]; 
unsigned char b = pPixels[(width*y+x) * 4 + 0]; 

//clean up the bitmap and buffer unless you still need it 
DeleteObject(hBmp); 
delete [] pPixels; // delete the array of objects 

così in breve, i lpvBits fuori parametro è il puntatore al pixel , ma se è solo 1 pixel è necessario suggerisco di usare getPixel per

Problemi correlati