C++ Pixel Grabber 错误的 RGB 值

标签 c++ windows colors rgb

我的像素采集器(我从在线源修改了它)但是 RGB 值不对..有人可以看看这个,也许可以修复它。

我正在尝试从屏幕上抓取所有像素并尽快将值转换为 RGB。

#include "iostream"
#include <Windows.h>
using namespace std;

HDC hdc, hdcTemp;
int x, y;


void PixelFunction();   // Get the pixel rgb function

int main()
{
    PixelFunction();
    ReleaseDC(HWND_DESKTOP, hdc);
    cout<<"done";
    getchar();
    return 0;
}


void PixelFunction()
{
    BYTE* bitPointer;
    int red, green, blue, alpha;

    hdc = GetDC(HWND_DESKTOP);
    int MAX_WIDTH = GetDeviceCaps(hdc, HORZRES);
    int MAX_HEIGHT = GetDeviceCaps(hdc, VERTRES);

    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 = MAX_WIDTH * 4 * MAX_HEIGHT;
    bitmap.bmiHeader.biClrUsed = 0;
    bitmap.bmiHeader.biClrImportant = 0;
    HBITMAP hBitmap2 = CreateDIBSection(hdcTemp, &bitmap, DIB_RGB_COLORS, (void**)(&bitPointer), NULL, NULL);
    SelectObject(hdcTemp, hBitmap2);
    BitBlt(hdcTemp, 0, 0, MAX_WIDTH, MAX_HEIGHT, hdc, 0, 0, SRCCOPY);
    for (int i=0; i<MAX_HEIGHT; i ++)
    {
        for (int ii=0; ii<MAX_WIDTH; ii++)
        {

            {
                blue = (int)bitPointer[ii];
                green = (int)bitPointer[ii+1];
                red = (int)bitPointer[ii+2];
                alpha = (int)bitPointer[ii+3];

                cout << "Red " << red << ".\n";
                cout << "Green " << green << ".\n";
                cout << "Blue " << blue << ".\n";
                SetCursorPos(ii, i);

                Sleep(500);
            }
        }

    }





}

最佳答案

您正在取消引用具有无意义偏移的位图指针。 我不太熟悉位图文件格式,但您可能需要类似以下内容:

blue = (int)bitPointer[i*MAX_WIDTH + ii];
green = (int)bitPointer[i*MAX_WIDTH + ii + 1];
red = (int)bitPointer[i*MAX_WIDTH + ii + 2];
alpha = (int)bitPointer[i*MAX_WIDTH + ii + 3];

目前,您只会解决第一行。

关于C++ Pixel Grabber 错误的 RGB 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18332289/

相关文章:

c++ - 从 C++ 函数返回不同的类型

c++ - 从 MySQL 获取 UTF-8 数据到 Linux C++ 应用程序

windows - 我需要一个批处理文件来生成*仅*文件名的列表

algorithm - Mysql 确定最接近颜色匹配的算法

r - 从 R 中的图像中提取前 2-3 个十六进制颜色

colors - 肤色检测

c++ - 更改应用程序文件的名称(.C 文件)

c++ - 如何公开大量 protected 内部类函数?

windows - 一个文件两个不同的输出 - Windows Server 2012

windows - 想要将 4 个用户输入传递给 Windows 批处理文件中的 4 个 SET 命令