0

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!

Tom Weiss
  • 11
  • 4

1 Answers1

0

Which kind of d3d application do you use? if the application use an Overlay surface, you can't get anything with code above. Overlay surface is widely used in Video players, it's totally different with normal surfaces in DirectX, the normal screen shot software can only catch data from primary surface, and Microsoft didn't provide any public interface to get data from Overlay surfaces, but some software can do this, the most common way is to hook DirectX, that's a different topic.

If your d3d application didn't use Overlay surface, you can use DiretX to get the data from screen, then get the pixel from the screen data you want.

  1. Use CreateOffscreenPlainSurface to create an offscreen surface
  2. Use GetFrontBufferData to get the data from screen
  3. Lock the surface and read the pixel to get the color
zdd
  • 8,258
  • 8
  • 46
  • 75
  • the easiest way i think is to take a screen shot(Use print screen key on keyboard) of your d3d application, if the screen shot is blank, that's a Overlay surface. – zdd Jul 02 '13 at 09:15
  • I did the test, It wasn't blank... I got the screenshot! Any example code that can help me get to the front buffer? Thanks! – Tom Weiss Jul 02 '13 at 11:44
  • you can google capture screen with directx, there are lots of result, take a look at this one http://www.codeproject.com/Articles/5051/Various-methods-for-capturing-the-screen – zdd Jul 02 '13 at 12:15