This function is used to get the display buffer of the graphics device.

DWORD* GetImageBuffer(IMAGE* pImg = NULL);



The drawing device pointer. If NULL, represents the default graphics window.

Return Value

The display buffer of the graphics device.


The display buffer pointer that is obtained can read and write directly.

In the display buffer, each point takes up 4 bytes, so the size of the display buffer is the size of the display buffer, the width is s. Pixels are arranged in the display buffer in order from left to right, top to down. Do not cross the border by accessing the display buffer, as this can have unpredictable consequences.

Displays the structure of each point in the buffer corresponding to the RGBTRIPLE type:

struct RGBTRIPLE {
	BYTE rgbtBlue;
	BYTE rgbtGreen;
	BYTE rgbtRed;

RGBTRIPLE is represented in memory by 0xrrggbb (bb-blue, gg-green, rr-red), while the commonly used COLORREF is represented in memory as 0xbbggrr. Note that the red and blue of the two are the opposite, please swap bGR macros for red and blue.

If you manipulate the display buffer of the graphics window, after the operation is complete, perform FlushBatchDraw() to make the operation take effect.


The following code draws the blue of the gradient by displaying the buffer directly:

#include <graphics.h>
#include <conio.h>

int main()
	// Initialize the graphics window
	initgraph(640, 480);

	// Get a pointer to the display buffer
	DWORD* pMem = GetImageBuffer();

	// Assigning a value directly to the display buffer
	for(int i = 0; i < 640 * 480; i++)
		pMem[i] = BGR(RGB(0, 0, i * 256 / (640 * 480) ));

	// Make the display buffer effective (Note: The action points to the IMAGE display buffer does not require this statement)

	// Press any key to exit
(贡献者:Krissi  编辑