Differences between Windows GDI and GDI + (instance profiling)

Source: Internet
Author: User

1. Overview

The full name of GDI is the graphics device interface, that is, the graphical device interface. A bridge between graphical display and physical devices. The GDI interface is based on functions.ProgramStaff effort-saving, but programming is still troublesome. For example, to display a bitmap, we need to perform a series of operations such as "create bitmap, read bitmap file information, enable scenario devices, and change the color palette. However, with GDI +, the tedious steps are simplified again. As the name suggests, GDI + is the enhanced version of GDI, which is a new interface provided by Microsoft in the operating system after Windows 2000.

2. The main functions of GDI +: (1) 2D vector graphics: GDI + provides a class (or struct) for storing the information of graphic elements) class that stores information about the drawing method of graphical elements and the class that actually draws;

(2) Image Processing: Most images are difficult to define as a set of straight lines and curves and cannot be processed using 2D vector images. Therefore, GDI + provides bitmap, image, and other classes for us to display, operate, and save BMP, JPG, GIF, and other image formats.

(3) text display: GDI + supports displaying text with various fonts, font sizes, and styles. Compared with GDI, GDI + is an object-oriented application interface based on the C ++ class, so it is easier to use. The core of GDI is the device context. The GDI function depends on the device context handle, and its programming method is based on the handle. GDI + does not need to depend on the handle or device context at all times, you only need to create a graphics object to call its member functions in object-oriented mode for graphic operations. The programming method is based on objects.

3. before using the device context to draw a line, you must call SelectObject to associate the pen object with the device context. Then, the pen is used for all lines drawn in the device context until another pen is selected. Use GDI to draw a lineCodeAs follows:

 
// Todo: add your command handler code here cclientdc clientdc; // Target DC cpen pen (ps_solid, 1, RGB (0, 0,255); clientdc. selectObject (pen. getsafehandle (); // starts to draw clientdc. moveTo (0, 0) clientdc. lineto (rect. right, 0); clientdc. selectObject (oldobject );

From the code above, we can see that in GDI programming, almost all operations are centered around the device context DC. Indeed, this is exactly the characteristic of GDI programming! The device context is a structure used by Windows. The context of a specific device must be obtained before all GDI operations. The cclientdc (this) Statement in the function completes this function. The general operation steps for graphics and image processing using GDI are as follows: 1. Obtain the DC of the specified window. 2. Determine the coordinate system used and the ing method. 3. Image, image, or text processing. 4. Release the used DC. However, in GDI +, you only need to pass the pen object directly as a parameter to the drawline and other methods of the graphics class, instead of associating the pen object with the graphics object. 4. The following code is used to draw an instance using the GDI + draw line method:

 
// Todo: add your command handler code here cclientdc clientdc (this); // create the graphics object graphics (clientdc); // create the PEN mypen; mypen. setwidth (1); // draw the X axis mypen. setcolor (color: Blue); graphics. drawline (& mypen, 0, 0, rect. right, 0 );

(1) create a graphics object: The graphics object represents the GDI + drawing surface and is used to create a graphic image. (2) draw lines and shapes, present text or display and operate images using graphics objects. Compared with GDI, GDI + has a series of new functions: gradient brushes, cardinal splines, and persistent path objects) transformations & matrix object, scalable regions, alpha blending, and rich image formats. Next, we will use the actual code to implement the new features of GDI +. 4.1 gradient painting brush (GDI + provides linear gradient painting brush and path gradient painting brush for filling graphics, paths and areas. Linear gradient Paint Brush uses gradient color to fill the image. When you fill the image with a path gradient Paint Brush, you can specify the way in which the color of the paint brush changes from one part of the image to another. For example, we can only specify the center color and edge color of the image. When the painting brush moves outward from the middle of the image, the painting brush gradually changes from the center color to the edge color.)

// Todo: add your command handler code herecclientdc clientdc (this); crect rect; getclientrect (& rect); // create the graphics object graphics (clientdc ); // create a gradient Paint Brush lineargradientbrush LGB (point (0, 0), point (rect. right, rect. bottom), color: blue, color: Green); // fill graphics. fillrectangle (& LGB, 0, 0, rect. right, rect. bottom );

4.2 base splines (base splines refer to a series of separate curves that are connected to form a larger curve. A Spline is specified by an array of points (point struct) and passes through each vertex in the array. The base spline smoothly passes through every point in the array (no sharp angle appears), so it is more accurate than the path created by using a straight line connection .)

// Todo: add your command handler code here cclientdc clientdc (this); // create the graphics object graphics (clientdc); point points [] = {point (0, 0 ), point (100,200), point (200, 0), point (300,200), point (400, 00)}; // draw a line for (INT I = 0; I <4; I ++) {graphics. drawline (& pen (color: blue, 3), points [I], points [I + 1]);} // draw a line graphics using a base spline. drawcurve (& pen (color: Red, 3), points, 5 );

4.3 deformation and matrix objects (GDI + provides a matrix object, which is a powerful tool that makes deformation (rotation, translation, scaling, etc.) simple and flexible, the matrix object must be used together with the object to be deformed. For the graphicspath class, we can use its member function transform to receive matrix parameters for deformation .)

// Todo: add your command handler code here cclientdc clientdc (this); // create the graphics object graphics (clientdc); graphicspath path; Path. addrectangle (rect (250, 20, 70, 70); graphics. drawpath (& pen (color: Black, 1), & Path); // draw a rectangle before applying the deformation matrix // path deformation matrix matrix1, matrix2; matrix1.rotate (45.0f ); // rotate the path 45 degrees clockwise. transform (& matrix1); // The graphics deformation of the application. drawpath (& pen (color: Red, 3), & Path); matrix2.scale (1.0f, 0.5f); // convert it to the parallelogram rule path. transform (& matrix2); // The graphics deformation of the application. drawpath (& pen (color: blue, 3), & Path );

4.4 supports a wide range of image formats (GDI + provides image, bitmap, and Metafile classes to facilitate the loading, operation, and storage of image formats. The image formats supported by GDI + include BMP, GIF, JPEG, EXIF, PNG, Tiff, icon, WMF, and EMF. Almost all common image formats are supported .)

 

From: http://edu.292775.com/windows/201211/16377.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.