I got that code to get the pixel color from current mouse position.
It works well but the only problem is, I can't get it from an d3d application...
I tried it few times, but it only get only black color -
Red: 0
Green: 0
Blue: 0
Here's my code -
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <d3d9.h>
HWND hWindow;
HDC hScreen;
HDC hdcMem;
HBITMAP hBitmap;
HGDIOBJ hOld;
int sX, sY, x, y;
BYTE* sData = 0;
POINT cursorPos;
int main()
{
int Red, Green, Blue;
hScreen = GetDC(hWindow);
sX = GetDeviceCaps(hScreen, HORZRES);
sY = GetDeviceCaps(hScreen, VERTRES);
hdcMem = CreateCompatibleDC (hScreen);
hBitmap = CreateCompatibleBitmap(hScreen, sX, sY);
BITMAPINFOHEADER bm = {0};
bm.biSize = sizeof(BITMAPINFOHEADER);
bm.biPlanes = 1;
bm.biBitCount = 32;
bm.biWidth = sX;
bm.biHeight = -sY;
bm.biCompression = BI_RGB;
bm.biSizeImage = 0; // 3 * sX * sY;
while (1) {
hOld = SelectObject(hdcMem, hBitmap);
BitBlt(hdcMem, 0, 0, sX, sY, hScreen, 0, 0, SRCCOPY);
SelectObject(hdcMem, hOld);
free(sData);
sData = (BYTE*)malloc(4 * sX * sY);
GetDIBits(hdcMem, hBitmap, 0, sY, sData, (BITMAPINFO*)&bm, DIB_RGB_COLORS);
GetCursorPos(&cursorPos);
x = cursorPos.x;
y = cursorPos.y;
Red = sData[4 * ( (y * sX) + x) +2];
Green = sData[4 * ( ( y * sX) + x) +1];
Blue = sData[4 * ( (y * sX) + x)];
printf("\nRed: %d\nGreen: %d\nBlue: %d\n", Red, Green, Blue);
Sleep(300);
}
}
Thanks!