[英]C++ Pixel Grabber wrong RGB values
my pixel grabber (I modified it from online source) But the RGB values are off.. Could someone take a look at this, and maybe fix it. 我的像素采集卡(我从在线来源对其进行了修改),但是RGB值已关闭。
I'm trying to grab all pixels from the screen and turn values into RGB as fast as possible. 我正在尝试从屏幕上获取所有像素并将值尽快转换为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);
}
}
}
}
You're dereferencing the bitmap pointer with nonsensical offsets. 您正在用无意义的偏移量取消引用位图指针。 I'm not really familiar with the bitmap file format, but you'll probably need something like:
我对位图文件格式不是很熟悉,但是您可能需要类似以下内容:
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];
Currently, you will only ever address the top row. 目前,您只会在最上面一排。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.