问题
i am doing a program where you take a screenshot of a window and then scan every pixel of that picture. But I have a problem assigning RGBQUAD array to the taken screen. Every pixel has the same RGB which is 205. Here is a piece of my code:
RGBQUAD *pixel = malloc((ssWidth * ssHeight)* sizeof(RGBQUAD));
hdcScreen = GetDC(gameHandle);
hdc = CreateCompatibleDC(hdcScreen);
hBmp = CreateCompatibleBitmap(hdcScreen, ssWidth, ssHeight);
SelectObject(hdc, hBmp);
BitBlt(hdc, 0, 0, ssWidth, ssHeight, hdcScreen, xCenter, yCenter, SRCCOPY);
GetDIBits(hdc, hBmp, 0, ssHeight, pixel, &bmpInfo, DIB_RGB_COLORS);
int p = -1;
for(y_var = 0; y_var < ssWidth; y_var++)
{
for(x_var = 0; x_var < ssHeight; x_var++)
{
if(ComparePixel(&pixel[++p]))
{
SetCursorPos(xCenter + x_var + 3, yCenter + y_var + 3);
}
}
}
bool ComparePixel(RGBQUAD *pixel)
{
printf("%d, %d, %d\n"; pixel -> rgbRed, pixel -> rgbGreen, pixel -> rgbBlue);
return false;
}
ComparePixel(RGBQUAD *pixel) function just checks the RGB values. How do i assign the RGBQUAD to the bitmap of the screenshot?
回答1:
Multiple issues.
The
RGBQUAD **pixel = malloc(...
andfree(*pixel)
appear to be the problem. I think you wantRGBQUAD *pixel = malloc((ssWidth * ssHeight)* sizeof(RGBQUAD));
(only 1*
)Suspect the
pixels
inGetDIBits()
s/bpixel
.I think you want
y_var = 0;
(x_var = 0; also)ComparePixel()
is not defined, but I think you want something closer toif(ComparePixel(pixel[x_var+(y_var*ssWidth)], the_pixel_to_compare_against))
The
free(*pixel);
s/b _after the 2 for loops and should befree(pixel)
;
来源:https://stackoverflow.com/questions/17271944/c-c-assign-rgbquad-array-to-a-bitmap