The Wayback Machine - https://web.archive.org/web/20240531172722/https://www.geeksforgeeks.org/cpp-program-to-read-screen-pixels/
Open In App

C++ Program to Read Screen Pixels

Last Updated : 27 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Obtaining the color information about the screen pixels is a common operation performed by automation software, which relies on activity on the screen. The process is trivial, and such functionality is incorporated into programming languages with the help of Image/Window reading libraries. This article will teach you how to read screen pixels in C++. The <windows.h> header file could help us to get pixel color.

GetPixel Function

A call to the getpixel function would be made to obtain the color value of the screen pixel, and the coordinates at which the pixel value is to be read would be passed as an argument. 

Syntax:

COLORREF GetPixel([in] HDC hdc, [in] int a, [in] int b);

Parameters:

  • hdc:  handle to a device context (device at which the pixels are to be read. For example, Monitor) 
  • a:  The x-coordinate, in logical units, of the pixel is to be examined.
  • b:  The y-coordinate, in logical units, of the pixel is to be examined.

Return value: The return value is the COLORREF value that specifies the RGB of the pixel. If the pixel is outside the clipping region, the return value is CLR_INVALID.

Reading Screen Pixels

In the following example, the pixel value for a single pixel will be obtained. This method could be extended to the whole display (using a loop) to obtain the color value of a range of pixels. 

Example:

C++




// C++ Program to implement
// Read Screen Pixels 
#include <windows.h>
#include <iostream>
  
using namespace std;
  
int main()
{
      // Assigning the device context to the current output device
      HDC dng = GetDC(NULL);
    
      // Calling the getpixel function and passing in the device context and coordinates
      // The coordinates should be inside the max resolution of the device context 
      // ex. screen coordinate X < 1919; in case of a full HD display
      COLORREF c = GetPixel(dng, 10, 150);
    
      // Beautifying the data to output as a tuple containing the RGB values
      // The GetRValue function returns the Red color value of the pixel
      // The GetGValue function returns the Green color value of the pixel
      // The GetBValue function returns the Blue color value of the pixel
      cout<<"(";
      cout<<(int)GetRValue(c)<<", ";
      cout<<(int)GetGValue(c)<<", ";
      cout<<(int)GetBValue(c)<<")";
    
      // Releasing the Handle
      ReleaseDC(NULL, dng);
}


Output:

The output of the program

The output of the program

Explanation: Firstly the device context of the display is obtained using the GetDC function with the NULL argument (for the screen). Then a call to the GetPixel function is made, and the device context, along with the coordinates (horizontal and vertical) of the pixel, is sent as an argument. The return data is stored in a COLORREF variable. Later, the Red, Green, and Blue channel values of the pixel are obtained via the GetRValue, GetBValue, and GetGValue functions, which are typecasted to make them appropriate for output. In the end, the display context is released using the ReleaseDC function. 



Similar Reads

C++ program to read file word by word
Given a text file, extract words from it. In other words, read the content of file word by word. Example : Input: And in that dream, we were flying. Output: And in that dream, we were flying. Approach : 1) Open the file which contains string. For example, file named "file.txt" contains a string "geeks for geeks". 2) Create a filestream variable to
1 min read
C++ Program to Make a File Read-Only
Here, we will build C++ Program to Make a File Read-Only using 2 approaches i.e. Using ifstreamUsing fstreamC++ programming language offers a library called fstream consisting of different kinds of classes to handle the files while working on them. The classes present in fstream are ofstream, ifstream, fstream. 1. Using "ifstream"The output of the
2 min read
C++ Program to Read Content From One File and Write it Into Another File
Here, we will see how to read contents from one file and write it to another file using a C++ program. Let us consider two files file1.txt and file2.txt. We are going to read the content of file.txt and write it in file2.txt Contents of file1.txt: Welcome to GeeksForGeeks Approach: Create an input file stream object and open file.txt in it.Create a
2 min read
C++ Program to Read and Print All Files From a Zip File
In this article, we will explore how to create a C++ program to read and print all files contained within a zip archive. What are zip files? Zip files are a popular way to compress and store multiple files in a single container. They use an algorithm to decrease the size of the data they store. They also contain information about file type, name, s
3 min read
Full screen OpenCV / GtK application in C++ running on Raspberry PI
Introduction C++, OpenCV and Gtk are a nice triplet to build applications that run on a Raspberry PI, taking images from the camera, process them, display them and have an unlimited user interface. In this article, I will show you a naive path to display camera captures to a full-screen window. In a second article, I will amend some of the shortcom
9 min read
How to Read and Print an Integer value in C++
The given task is to take an integer as input from the user and print that integer in C++ language. In this article, we will learn how to read and print an integer value. In the below program, the syntax and procedures to take the integer as input from the user is shown in C++ language. Standard Input StreamThe user enters an integer value when ask
2 min read
Read a record from a File in C++ using seekg() and tellg()
Given that a binary file "student.data" is already loaded in the memory of the computer with the record of 100 students, the task is to read the Kth record and perform some operations.seekg() is a function in the iostream library (part of the standard library) that allows you to seek to an arbitrary position in a file. It is used in file handling t
2 min read
Read a string after reading an integer
In this article, we will discuss how to read a string after reading an integer. Program 1: Below is the program that inputs a string with spaces just after taken an input of an integer: C/C++ Code // C++ program that inputs a string // with spaces just after taken an // input of an integer #include &lt;iostream&gt; #include &lt;string&gt; using nam
2 min read
How to Read and Parse Json File with RapidJson?
RapidJSON is a high-performance JSON library for C++. It provides a fast and easy-to-use interface for parsing and generating JSON. It is small but complete. It supports both SAX and DOM style API. Also, it is self-contained and header-only. It does not depend on external libraries such as BOOST. It even does not depend on STL. Here is an example o
5 min read
RapidJSON - File Read/Write in C++
RapidJSON is a C++ library for parsing and generating JSON (JavaScript Object Notation) data. It is designed for high performance and can handle very large JSON documents. RapidJSON supports both reading and writing JSON data, as well as validation and manipulation of JSON objects. It also includes support for parsing and generating JSON in a strea
6 min read