原始/直接访问BITMAPINFO(HBITMAP)中的像素数据

时间:2016-03-03 03:27:22

标签: c pointers memory-leaks bitmap gdi

我正在试图弄清楚如何从屏幕获取原始像素信息。到目前为止,我一直在将屏幕捕获到HBITMAP,填充BITMAPINFO,然后创建此BITMAPINFO变量的指针,以直接从内存中读取。 我知道标题必须从文件中“删除”,所以我将指针直接前进到位图数据(将sizeof(MyBMInfo2-> bmiHeader)添加到我的指针偏移量)。我也知道这个位图是反转/自顶向下的,第一个像素位于原始数据的末尾。我需要弄清楚如何从图像中的给定X和Y中提取R G和B字节,这就是我无法做到的。 所以我问你先生一个灯光,一个片段或任何提示,可以帮助我从gdi32重新创建Bitmap.GetPixel(x,y)的脏尝试(这么慢,我需要一个更好的)。

到目前为止,我的源代码的代码段

...
HDC hCaptureDC  = CreateCompatibleDC(hdc);
HBITMAP hBitmap = CreateCompatibleBitmap(hdc, nScreenWidth, nScreenHeight);
HGDIOBJ hOld = SelectObject(hCaptureDC, hBitmap);
BOOL bOK = BitBlt(hCaptureDC,0,0,nScreenWidth, nScreenHeight, hdc,0,0,SRCCOPY|CAPTUREBLT);
SelectObject(hCaptureDC, hOld); // always select the previously selected object once done 
DeleteDC(hCaptureDC);

BITMAPINFO MyBMInfo = {0};
MyBMInfo.bmiHeader.biSize = sizeof(MyBMInfo.bmiHeader);

// Get the MyBMInfo structure from the bitmap
if(0 == GetDIBits(hdc, hBitmap, 0, 0, NULL, &MyBMInfo, DIB_RGB_COLORS)) {
    printf("error\n");
}

BITMAPINFO* MyBMInfo2 = &MyBMInfo;
BYTE* bitmapBits=(BYTE*)MyBMInfo2+sizeof(MyBMInfo2->bmiHeader);
//So... how do I acess X and Y RGB bytes now? xD
...

顺便说一下....还有其他更直接的方法可以做到这一点而不会引发内存保护错误吗?或者......另一种更快的方式? 谢谢。

- 编辑 -

使用Barmak的代码我已经弄清楚如何根据当前光标位置访问X和Y rgb,这里是源代码:

#include <windows.h>

int main()
{
    HWND hwnd = GetDesktopWindow();
    RECT rc;
    GetClientRect(hwnd, &rc);
    int width = rc.right;
    int height = rc.bottom;
    if (width < 1 || height < 1)
    {
        OutputDebugStringA("error\n");
        return 0;
    }

    HDC hdc = GetDC(hwnd);

    HDC hCaptureDC = CreateCompatibleDC(hdc);
    HBITMAP hBitmap = CreateCompatibleBitmap(hdc, width, height);
    HGDIOBJ hOld = SelectObject(hCaptureDC, hBitmap);
    BitBlt(hCaptureDC, 0, 0, width, height, hdc, 0, 0, SRCCOPY | CAPTUREBLT);
    SelectObject(hCaptureDC, hOld);

    BITMAPINFO MyBMInfo = { 0 };
    MyBMInfo.bmiHeader.biSize = sizeof(MyBMInfo.bmiHeader);

    BITMAPINFOHEADER bmpInfoHeader = { sizeof(BITMAPINFOHEADER) };
    bmpInfoHeader.biWidth = width;
    bmpInfoHeader.biHeight = height;
    bmpInfoHeader.biPlanes = 1;
    bmpInfoHeader.biBitCount = 32;

    DWORD size = ((width * bmpInfoHeader.biBitCount + 31) / 32) * 4 * height;
    BYTE *bits = malloc(size);

    if (GetDIBits(hdc, hBitmap, 0, height, bits, (BITMAPINFO*)&bmpInfoHeader, DIB_RGB_COLORS))
    {
        OutputDebugStringA("success\n");
        //you can use bits here

        //access bits from upper-left to lower-right corner
        POINT p;
        GetCursorPos(&p);
        int x = p.x;
        int y = p.y;

        int col = x;
        int row = height - y - 1;
        int index = (row * width + col) * 4;

        BYTE b = bits[index + 0];
        BYTE g = bits[index + 1];
        BYTE r = bits[index + 2];

        printf("r:%i g:%i b:%i \n",r,g,b);


    }
    else
    {
        OutputDebugStringA("error\n");
    }

    free(bits);

    DeleteDC(hCaptureDC);
    DeleteObject(hBitmap);
    ReleaseDC(hwnd, hdc);

    return 0;
}

非常感谢谢米拉尼先生。对于那些低估我的答案的人,请考虑撤回它吗? :) 和平。

1 个答案:

答案 0 :(得分:0)

GetDibBits需要一个缓冲区来接收这些位。您必须分配缓冲区,然后在不再需要时删除。

#include <windows.h>

int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
    HWND hwnd = GetDesktopWindow();
    RECT rc;
    GetClientRect(hwnd, &rc);
    int width = rc.right;
    int height = rc.bottom;
    if (width < 1 || height < 1)
    {
        OutputDebugStringA("error\n");
        return 0;
    }

    HDC hdc = GetDC(hwnd);

    HDC hCaptureDC = CreateCompatibleDC(hdc);
    HBITMAP hBitmap = CreateCompatibleBitmap(hdc, width, height);
    HGDIOBJ hOld = SelectObject(hCaptureDC, hBitmap);
    BitBlt(hCaptureDC, 0, 0, width, height, hdc, 0, 0, SRCCOPY | CAPTUREBLT);
    SelectObject(hCaptureDC, hOld);

    BITMAPINFO MyBMInfo = { 0 };
    MyBMInfo.bmiHeader.biSize = sizeof(MyBMInfo.bmiHeader);

    BITMAPINFOHEADER bmpInfoHeader = { sizeof(BITMAPINFOHEADER) };
    bmpInfoHeader.biWidth = width;
    bmpInfoHeader.biHeight = height;
    bmpInfoHeader.biPlanes = 1;
    bmpInfoHeader.biBitCount = 32;

    DWORD size = ((width * bmpInfoHeader.biBitCount + 31) / 32) * 4 * height;
    BYTE *bits = malloc(size);

    if (GetDIBits(hdc, hBitmap, 0, height, bits, (BITMAPINFO*)&bmpInfoHeader, DIB_RGB_COLORS))
    {
        OutputDebugStringA("success\n");
        //you can use bits here

        //access bits from lower-left to upper-right corner
        for (int row = 0; row < height; row++)
        {
            for (int col = 0; col < width; col++)
            {
                //for 32 bit image only:
                int index = (row * width + col) * 4;

                BYTE blue  = bits[index + 0];
                BYTE green = bits[index + 1];
                BYTE red   = bits[index + 2];
            }
        }

        //access bits from upper-left to lower-right corner
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                int col = x;
                int row = height - y - 1;
                int index = (row * width + col) * 4;

                BYTE b = bits[index + 0];
                BYTE g = bits[index + 1];
                BYTE r = bits[index + 2];
            }
        }

    }
    else
    {
        OutputDebugStringA("error\n");
    }

    free(bits);

    DeleteDC(hCaptureDC);
    DeleteObject(hBitmap);
    ReleaseDC(hwnd, hdc);

    return 0;
}