2012-01-25 12 views
5

Szukałem i zrozumiałem, że będę musiał użyć GetDIBits(). Nie wiem, co zrobić z parametrem wyjściowym LPVOID lpvBits.Jak uzyskać dostęp do koloru pikseli w bitmapie?

Czy ktoś mógłby mi to wyjaśnić? Potrzebuję uzyskać informacje o kolorach pikseli w dwuwymiarowej macierzy, aby móc pobrać informacje dla określonej pary współrzędnych (x, y).

Programuję w C++ przy użyciu Win32 API.

+0

możliwym duplikatu [GetDIBits i pętli pikseli za pomocą X, Y] (godz ttp: //stackoverflow.com/questions/3688409/getdibits-and-loop-through-pixels-using-x-y) –

Odpowiedz

1

Nie jestem pewien, czy to jest to, czego szukasz, ale GetPixel robi dość dużo, co trzeba ... przynajmniej ze mogę powiedzieć z opisem danej funkcji

+1

Zazwyczaj nie chcesz używać GetPixel do wyodrębniania wszystkich pikseli, robi się dość wolno. – pezcode

5

najpierw trzeba bitmapę i otwarty to

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 

tak w skrócie, lpvBits spośród parametrem jest wskaźnik do pikseli ale jeśli jest to tylko 1 piksel trzeba proponuję użyć getPixel do

Powiązane problemy