Read a pixel on a game screen (OpenGL or DirectX)

I want to read the color of a pixel at a given position in a game (like OpenGL or DirectX), a third-party application (this is not my game).

I tried to do this in C #, the code is great for reading the color of the desktop, windows, etc., but when I start the game, I get only # 000000, black pixel. I think this is because I do not read the correct "location" or something like that.

Does anyone know how to do this? I mentioned C #, but C / C ++ would be fine too.

+4
source share
2 answers

In basic steps: capture the texture of the rendered screen using the appropriate OpenGL or Directx command if the game is full-screen. For example, using glReadPixels you can get the pixel value in coordinates relative to the pixel coordinates of a window from the current associated framebuffer. If you are not full screen, you must combine the position of the window with the coordinates relative to the pixel windows.

Some examples:

 glBindFramebuffer(GL_FRAMEBUFFER, yourScreenFramebuffer); glReadPixels(/* your pixel X, your pixel Y, GLsizei width, 1 pixel wide, 1 pixel tall, GL_RGBA or GL_RGB, GL_UNSIGNED_BYTE, *where to store your pixel value */); 
0
source

Windows has, for example, GDI (graphical device interface): with GDI you can easily get the device context with HDC dc = GetDC(NULL); And then to read the pixel values using COLORREF color = GetPixel(dc, x, y); , y); . But be careful: after that you must free the device context (when all the GetPixel actions of your program are completed) using ReleaseDC(NULL, dc); - otherwise you will be a memory leak. See also here for more details.

However, for such tasks, I suggest you use: Auto-it . It is simple, easy to use, and fairly simple (after all, it is just designed for such operations).

 Local $color = PixelGetColor(200, 300) MsgBox(0, "The color is ", $color ) 
0
source

All Articles