Windows Graphics Device Interface (GDI)

Explore the core Windows API for 2D graphics and drawing.

Introduction to GDI

The Graphics Device Interface (GDI) is a core component of the Microsoft Windows operating system that provides a standardized way for applications to draw graphics and text on the screen and printers. It handles operations such as drawing lines, curves, shapes, text, and bitmaps.

Key Concepts: GDI utilizes a device context (DC) to manage drawing operations. Resources like pens, brushes, fonts, and bitmaps are managed within the DC to render output to various devices.

Core GDI Components

Common GDI Functions

Drawing Shapes

Text Rendering

Managing Graphics Objects

Color and Brushes

GDI supports various ways to define colors and brushes:

GDI vs. GDI+

While GDI is a foundational API, Microsoft also developed GDI+. GDI+ offers a more modern, object-oriented approach to graphics, supporting features like anti-aliasing, gradients, and advanced image manipulation. For new development, GDI+ is often recommended, but understanding GDI remains crucial for legacy applications and lower-level graphics control.

Example Snippet

Here's a conceptual example of drawing a red rectangle using GDI:


#include <windows.h>

// Assume hdc is a valid Device Context
HDC hdc;
RECT rect = { 10, 10, 100, 100 }; // Coordinates for the rectangle
HBRUSH hBrush = CreateSolidBrush(RGB(255, 0, 0)); // Red brush

// Select the brush and draw the rectangle
HBRUSH hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);

// Restore the old brush and clean up
SelectObject(hdc, hOldBrush);
DeleteObject(hBrush);