Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GetPixel Function RGB Accuracy

Status
Not open for further replies.

olie

Programmer
Aug 21, 2002
9
US
Hello,

I was writing a program that "sniffs" pixels and stores them into a file for future reference by other programs using the mouse cursor as the area where the pixel is to be retrieved. I was trying to use this program to get the Red, Green, and Blue values which GetPixel retrieves off of a game known as Diablo 2. I know this game to be rich in unique RGB colors due to its highly accelerated graphics. However, when I use my pixel sniffer, it seems to have a narrow scope of RGB identification and keeps retrieving the same RGB colors over and over again even though the RGB colors are actually very much different. I tried changing my resolution to 32 bit high but it had no effect. The same narrow scope of RGB colors came up again. Does anyone know how to fix this dilemma? For reference, here is the main coding for the pixel sniffer.

typedef struct tagPOINT { // pt
LONG x;
LONG y;
} POINT;
POINT pt;
BOOL get=GetCursorPos((LPPOINT(&pt)));
if (get == 0)
{
MessageBox(NULL, "Could not get mouse position", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
HWND handle=GetDesktopWindow();
if (handle == NULL)
{
MessageBox(NULL, "Could not get device content", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
HDC devcont=GetWindowDC(handle);
if (devcont == NULL)
{
MessageBox(NULL, "Could not get device content", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
COLORREF pixel=GetPixel(devcont, pt.x, pt.y);
if (pixel == CLR_INVALID)
{
MessageBox(NULL, "Could not extract pixel", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
LPCSTR name="sniffed.txt";
HANDLE file= CreateFile ( name
, GENERIC_READ | GENERIC_WRITE
, FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
if (file == INVALID_HANDLE_VALUE)
{
MessageBox(NULL, "Could not open sniffed.txt", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
DWORD charsWritten=0;
if ( !WriteFile ( (HANDLE(file))
, &pixel
, sizeof(pixel)
, &charsWritten
, NULL
)
)
{
MessageBox(NULL, "Could not write to sniffed.txt", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
 
How can you get pixels from your game window if you getting them from desktop? Look:

HWND handle=GetDesktopWindow();
if (handle == NULL)
{
...
}
HDC devcont=GetWindowDC(handle);

You need to get a handle of the games window and then try to get it's DC and pixels.

--- markus
 
That sounds great, but I have no idea how to get the Diablo 2 window handle. GetActiveWindow() doesn't seem to work. Do you have any other suggestions?
 
I can suggest you to setup a mouse hook and if you've already done it then use "Spy++" from Visual Studio to find out the name of Diablo's window name. Once you know, use FindWindow function to retrieve it's handle. I could give a code example but i am Delphi programmer so that wouldn't be much of a help to you i guess.

--- markus
 
Hey thanks, I think I have it now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top