2012-01-25 25 views
5

Busqué y entendí que tendré que usar GetDIBits(). No sé qué hacer con el parámetro de salida LPVOID lpvBits.¿Cómo acceder al color de píxel dentro de un mapa de bits?

¿Podría alguien explicarme por favor? Necesito obtener la información del color del píxel en una forma de matriz bidimensional para que pueda recuperar la información para un par de coordenadas particular (x, y).

Estoy programando en C++ usando Win32 API.

+0

posible duplicado de [GetDIBits y bucle a través de píxeles usando X, Y] (h ttp: //stackoverflow.com/questions/3688409/getdibits-and-loop-through-pixels-using-x-y) –

Respuesta

1

no estoy seguro si esto es lo que estás buscando, pero GetPixel hace bastante más de lo que necesita ... al menos desde lo que puedo decir de la descripción de la función

+1

Por lo general, no desea utilizar GetPixel para extraer todos los píxeles, se vuelve bastante lento. – pezcode

5

Lo primero que necesita un mapa de bits y abierto que

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 

por lo que en breve, los lpvBits fuera de los parámetros es el puntero a los píxeles pero si es sólo 1 píxel que necesita es mejor utilizar getPixel a

Cuestiones relacionadas