c/c++ assign RGBQUAD array to a bitmap

丶灬走出姿态 提交于 2021-01-28 05:20:27

问题


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.

  1. The RGBQUAD **pixel = malloc(... and free(*pixel) appear to be the problem. I think you want RGBQUAD *pixel = malloc((ssWidth * ssHeight)* sizeof(RGBQUAD)); (only 1 *)

  2. Suspect the pixels in GetDIBits() s/b pixel.

  3. I think you want y_var = 0; (x_var = 0; also)

  4. ComparePixel() is not defined, but I think you want something closer to if(ComparePixel(pixel[x_var+(y_var*ssWidth)], the_pixel_to_compare_against))

  5. The free(*pixel); s/b _after the 2 for loops and should be free(pixel);



来源:https://stackoverflow.com/questions/17271944/c-c-assign-rgbquad-array-to-a-bitmap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!