Search MSDN
Graphics Device Interface (GDI) Overview
The Graphics Device Interface (GDI) is a core component of the Microsoft Windows operating system that provides a device-independent interface for drawing graphics and text on output devices such as monitors, printers, and plotters. GDI abstracts the complexities of hardware-specific graphics operations, allowing developers to create rich visual experiences without needing to write code for each individual graphics device.
Key Concepts
Device Context (DC)
A device context is a structure that contains information about the drawing attributes of a device. It's essentially a handle that your application uses to refer to the drawing surface and its associated properties, such as pen color, brush style, font, and display mode. You must obtain a device context before you can perform any drawing operations.
Graphical Objects
GDI uses several graphical objects to control drawing operations:
- Pens: Used for drawing lines, curves, and borders. You can specify their color, width, and style (solid, dashed, etc.).
- Brushes: Used for filling areas, such as rectangles, ellipses, and polygons, and for painting text. Brushes can be solid colors, hatched patterns, or bitmaps.
- Bitmaps: Raster graphics images that can be displayed or manipulated.
- Palettes: Used to manage the colors available for display devices that have a limited color resolution.
- Fonts: Define the typeface, size, and style of text.
Drawing Functions
GDI provides a rich set of functions for:
- Line and Curve Drawing: Functions like
MoveToEx,LineTo, andPolylineTo. - Shape Drawing: Functions like
Rectangle,Ellipse,Polygon, andPie. - Text Rendering: Functions like
TextOutandExtTextOutfor drawing strings. - Bitmap Operations: Functions for copying, manipulating, and displaying bitmaps.
- Color Management: Functions for managing color palettes and transformations.
Note on GDI+
While GDI is a foundational graphics API, Windows also offers GDI+ (Graphics Device Interface Plus). GDI+ provides a higher-level, object-oriented API for 2D graphics, image processing, and text rendering, offering more advanced features like anti-aliasing, gradients, and support for various image formats. For new development, especially for richer visual applications, GDI+ is often the preferred choice.
Common Use Cases
- Drawing custom user interface elements.
- Creating charts and graphs.
- Rendering vector-based graphics.
- Performing image manipulation.
- Sending graphical output to printers.
Related API References
Tip
Always release graphical objects and device contexts when they are no longer needed to prevent resource leaks.