Differences Between GDI and WPF (original) (raw)
The Windows Presentation Foundation (WPF) API is fundamentally different from the Graphics Device Interface (GDI) and GDI+ APIs, requiring that many aspects of programming be approached in a different way. This topic briefly outlines the major differences in two major areas, including differences as a renderer and as a framework.
Renderer
As a renderer, WPF enables you to use:
- Animations - WPF's built-in support for timing and screen repainting
- Gradients - blend colors along an axis to add highlights, shadows and 3D effects
- Antialiasing - reduce jaggies of diagonals and curves with antialiasing
- UI scaling - scale the entire user interface instead of individual drawings
- Translucency - increase useable screen area by employing translucency
- Vector graphics - WPF has built-in support for vector graphics that practically provides infinite zoom without loss of quality
- Audio and video - WPF has built-in renderers for multimedia playback
- Device independence - using device independent units means that WPF applications will automatically scale to the dpi being used by the device
The following table summarizes these differences between GDI-based and WPF-based rendering:
Feature | GDI/GDI+ (Windows Forms) | WPF |
---|---|---|
DLLs used | System.dll System.Drawing.dll System.Windows.Forms.dll | WindowsBase.dll PresentationCore.dll PresentationFoundation.dll |
Windows | Many windows | One window |
Units | Device-dependent unit (pixels) | Device-independent unit (1/96-inch) |
Control positioning | Absolute | Absolute, Dynamic, Data-bound |
Image is formed by | Closely spaced rows of dots (bitmap, raster-based) | Mathematical equations (vector-based) |
Rendering engine | DirectShow | DirectX (Direct3D) Media Foundation |
Mode | Immediate - the application repaints the area that becomes invalidated | Retained - the application keeps the drawing information and the system does the repainting |
Painting | Clipping - bounds determined and painting occurs there | Rendering performed from back to front - components paint over each other |
Pen and brush | Current pen and current brush | Appropriate pen or brush provided with each drawing call |
Painting region | Minimize the region to be painted | Do not need to minimize |
Events | Events | RoutedEvents |
Video and Audio | Requires a player like Windows Media Player | Built-in support |
Windows
Applications built using the GDI/GDI+ API use many windows, and reside under a parent window (MDI). Applications built in WPF have only one window.
Unit of Measure
Applications that use the GDI/GDI+ API use the device-dependent unit (pixel) as the unit of measure. In these applications, as the resolution of the display device increases, the resulting logical image size decreases. Applications built for WPF use the device-independent unit (1/96-inch) as the unit of measure. When the DPI of the device is 96, the two images are equivalent.
Control Positioning
Applications built with GDI+ use absolute positioning. When the parent is resized the child elements do not get resized automatically. Applications built for WPF can use absolute, dynamic, or data-bound positioning. With either absolute or dynamic positioning the controls are positioned relative to the parent element.
Image Basis
Images created in GDI/GDI+ are pixel-based, raster images. Those created in WPF can be scalable, vector images.
Rendering Engine
Both GDI and GDI+ are built upon Win32. GDI is based on the concept of a device context (DC), where applications obtain handles to the device context, and use the handles to interact with the device. GDI+ is a wrapper around GDI that creates a C++ Graphics
object. On the other hand, WPF is built upon DirectX, which means it can take advantage of hardware acceleration when performing drawing operations.
Rendering Mode
With GDI and GDI+, rendering uses immediate rendering: the application repaints the area that becomes invalidated. In WPF, rendering uses retained rendering: the application keeps track of the drawing information but the system performs the painting.
Painting
With GDI and GDI+, clipping is used to determine the bounds of the area that has become invalidated and needs to be painted. In WPF, painting is performed from back to front and components paint over each other.
Pens and Brushes
GDI and GDI+ use the concept of a current brush and current pen. In WPF, the brush and pen must be passed with each drawing call.
Paint Region Optimization
Paint region optimization is an important part of painting with GDI or GDI+. In WPF, it is not considered.
Events
GDI and GDI+ use events and event handler delegates using subscription/notification. In contrast, WPF uses bubbling, tunneling, and direct event notification, and the event travels up and down the VisualTree
.
Video and Audio
Direct support for video and audio is not provided by GDI or GDI+, but must be obtained through a media player like Windows Media Player. WPF directly supports video and audio via the MediaElement Class.
Framework
As a framework, WPF enables you to use:
- XAML - eliminate creating mock-ups in Visio or PowerPoint. Using XAML it is possible to create the user-interface for an application directly so it is ready to be coded with the business logic.
- Styles and templates - use styles on any UI element to maintain visual consistency.
- Data binding - bind elements so they can present or interact with data.
- A tool to localize an application - although with Windows Forms it is possible to use either Winres.exe or Visual Studio to localize a form, in WPF one can use the LocBaml tool.