0

I'am looking for a faster alternative of GetPixel.

At the moment I have used this code that work:

COLORREF GetPixelOptimized(int x, int y)
{
    HWND hWnd = GetDesktopWindow();
    HDC hdc = GetDC(hWnd);

    RECT rect;
    GetWindowRect(hWnd, &rect);

    int MAX_WIDTH = rect.right - rect.left;
    int MAX_HEIGHT = rect.bottom - rect.top;

    HDC hdcTemp = CreateCompatibleDC(hdc);

    BITMAPINFO bitmap;
    bitmap.bmiHeader.biSize = sizeof(bitmap.bmiHeader);
    bitmap.bmiHeader.biWidth = MAX_WIDTH;
    bitmap.bmiHeader.biHeight = -MAX_HEIGHT;
    bitmap.bmiHeader.biPlanes = 1;
    bitmap.bmiHeader.biBitCount = 32;
    bitmap.bmiHeader.biCompression = BI_RGB;
    bitmap.bmiHeader.biSizeImage = 0;
    bitmap.bmiHeader.biClrUsed = 0;
    bitmap.bmiHeader.biClrImportant = 0;

    LPRGBQUAD bitPointer;
    HBITMAP hBitmap2 = CreateDIBSection(hdcTemp, &bitmap, DIB_RGB_COLORS, (void**)&bitPointer, 0, 0);

    HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcTemp, hBitmap2);
    BitBlt(hdcTemp, 0, 0, MAX_WIDTH, MAX_HEIGHT, hdc, 0, 0, SRCCOPY);

    LPRGBQUAD hex_color = &bitPointer[(MAX_WIDTH * y) + x];
    int red = hex_color->rgbRed;
    int green = hex_color->rgbGreen;
    int blue = hex_color->rgbBlue;

    SelectObject(hdcTemp, hbmpOld);
    DeleteObject(hBitmap2);
    DeleteDC(hdcTemp);

    ReleaseDC(hWnd, hdc);

     RGB(red, green, blue);

}

but is slower then GetPixel.

A possibly solution is use GetDIBits but I don't known how use it.

Can you please help me to optimize this code or suggest me a better way ?

Thanks !

marcoluca987
  • 59
  • 1
  • 5

0 Answers0