Create a simple Paintbrush application using various GDI components.
Aim: Create a simple Paintbrush application using various GDI components.
Theory: GDI is responsible for tasks such as drawing lines and curves, rendering fonts and handling palettes. It is not directly responsible for drawing windows, menus, etc.: that task is reserved for the user subsystem, which resides in user32.dll and is built atop GDI. GDI is similar to Apple’s classic QuickDraw.
Perhaps the most significant capability of GDI over more direct methods of accessing the hardware is its scaling capabilities, and abstraction of target devices. Using GDI, it is very easy to draw on multiple devices, such as a screen and a printer, and expect proper reproduction in each case. This capability is at the centre of all WYSIWYG applications for Microsoft Windows.
Simple games which do not require fast graphics rendering, such as Freecell or Minesweeper, use GDI. However, GDI cannot animate properly (no notion of synchronizing with the framebuffer) and lacks rasterization for 3D. Modern games use DirectX or OpenGL, which give programmers access to more hardware capabilities.
In Windows Vista, GDI applications running in the new compositing engine, Desktop Window Manager, will no longer be hardware-accelerated.
With the introduction of Windows XP, GDI was deprecated in favor of its successor, the C++ based GDI+ subsystem. GDI+ is a “next generation” 2D graphics environment, adding advanced features such as anti-aliased 2D graphics, floating point coordinates, alpha blending, gradient shading, more complex path management, intrinsic support for modern graphics-file formats like JPEG and PNG (which were conspicuously absent in GDI), and general support for composition of affine transformations in the 2D view pipeline. Use of these features is apparent in Windows XP’s user interface, and their presence in the basic graphics layer greatly simplifies implementations of vector-graphics systems such as Flash or SVG. The GDI+ dynamic library can be shipped with an application and used under older versions of Windows.
The Microsoft .NET class library provides a managed interface for GDI+ via the System.Drawing namespace.
GDI+ is similar (in purpose and structure) to Apple’s Quartz 2D subsystem, and the open-source libart and Cairo libraries.
Microsoft Windows GDI+ draws lines, rectangles, and other figures on a coordinate system. You can choose from a variety of coordinate systems, but the default coordinate system has the origin in the upper left corner with the x-axis pointing to the right and the y-axis pointing down. The unit of measure in the default coordinate system is the pixel.
default coordinate system
A computer monitor creates its display on a rectangular array of dots called picture elements or pixels. The number of pixels appearing on the screen varies from one monitor to the next, and the number of pixels appearing on an individual monitor can usually be configured to some extent by the user.
rectangular array of pixels
When you use GDI+ to draw a line, rectangle, or curve, you provide certain key information about the item to be drawn. For example, you can specify a line by providing two points, and you can specify a rectangle by providing a point, a height, and a width. GDI+ works in conjunction with the display driver software to determine which pixels must be turned on to show the line, rectangle, or curve. The following illustration shows the pixels that are turned on to display a line from the point (4, 2) to the point (12, 8).
line, as displayed by a rectangular array of pixels
Over time, certain basic building blocks have proven to be the most useful for creating two-dimensional pictures. These building blocks, which are all supported by GDI+, are given in the following list:
* Lines
* Rectangles
* Ellipses
* Arcs
* Polygons
* Cardinal splines
* Bzier splines
The Graphics class in GDI+ provides the following methods for drawing the items in the previous list: DrawLine, DrawRectangle, DrawEllipse, DrawPolygon, DrawArc, DrawCurve (for cardinal splines), and DrawBezier. Each of these methods is overloaded; that is, each method comes in several variations with different parameter lists. For example, one variation of the DrawLine method receives the address of a Pen object and four integers, while another variation of the DrawLine method receives the address of a Pen object and two Point object references.
The methods for drawing lines, rectangles, and Bzier splines have plural companion methods that draw several items in a single call: DrawLines, DrawRectangles, and DrawBeziers. Also, the DrawCurve method has a companion method, DrawClosedCurve, that closes a curve by connecting the ending point of the curve to the starting point.
All the drawing methods of the Graphics class work in conjunction with a Pen object. Thus, in order to draw anything, you must create at least two objects: a Graphics object and a Pen object. The Pen object stores attributes of the item to be drawn, such as line width and color. The address of the Pen object is passed as one of the arguments to the drawing method. For example, one variation of the DrawRectangle method receives the address of a Pen object and four integers as shown in the following code, which draws a rectangle with a width of 100, a height of 50 and an upper-left corner of (20, 10).
myGraphics.DrawRectangle(&myPen, 20, 10, 100, 50);
Conclusion: We have successfully implemented paint brush using GDI.
Leave a Reply