Overview of image and bitmap classes The image class of GDI + encapsulates BMP, GIF, JPEG, PNG, Tiff, WMF (Windows Metafile) and EMF (enhanced WMF) functions of image file transfer, format conversion, and simple processing. Bitmap is an image class inherited from the image class, which encapsulates common features of Windows bitmap operations. For example, bitmap: setpixel and bitmap: getpixel are used to read and write bitmap pixels respectively, which provides a possibility for image softening and sharpening. 3. drawimage Method Drawimage is the core method for display images in the graphics class of GDI +. Its heavy-duty functions include many. Common overload functions include: Status drawimage (image * image, int X, int y ); Status drawimage (image * image, const rect & rect ); Status drawimage (image * image, const point * destpoints, int count ); Status drawimage (image * image, int X, int y, Int srcx, int srcy, int srcwidth, int srcheight, unit srcunit ); (X, Y) is used to specify the position where the image is displayed. This position corresponds to the point in the upper left corner of the image. Rect is used to specify the rectangular area filled by the image. destpoints and count are used to specify the vertex and vertex count of a polygon respectively. If count is 3, the polygon is a parallelogram, And the other vertex is automatically given by the system. In this case, the data in destpoints corresponds to the vertex coordinates in the upper left, upper right, and lower left corner of the source image. Srcx, srcy, srcwidth, and srcheight are used to specify the position and size of the source image to be displayed. srcunit is used to specify the unit used. By default, pageunitpixel is used as the measurement unit. Call and display image files It is very easy to call and display image files in GDI +. Generally, you need to call an image file through image or bitmap to construct an object, and then call graphics :: the drawimage method displays all or part of the image at the specified position. For exampleCode: Void cex_gdiplusview: ondraw (CDC * PDC) { Cex_gdiplusdoc * pdoc = getdocument (); Assert_valid (pdoc ); Using namespace gdiplus; Graphics graphics (PDC-> m_hdc ); Image image (L "sunflower.jpg "); Graphics. drawimage (& image, 10, 10 ); Rect (130, 10, image. getwidth (), image. getheight ()); Graphics. drawimage (& image, rect ); } As shown in result 7.17, we can see that the results of the two drawimage operations are different, and the results should be the same. What is the problem? It turns out that drawimage scales automatically based on the device resolution when the display area is not specified, resulting in different display results. Of course, you can also use the bitmap class to call image files to construct a bitmap object. The results are the same. For example, the above Code can be changed: Bitmap BMP (L "sunflower.jpg "); Graphics. drawimage (& BMP, 10, 10 ); Rect (130, 10, BMP. getwidth (), BMP. getheight ()); Graphics. drawimage (& BMP, rect ); It should be noted that the image also provides the getthumbnailimage method to obtain a thumbnail pointer. After drawimage is called, the thumbnail can be displayed, which is extremely useful in image preview. For example, the following code: Graphics graphics (PDC-> m_hdc ); Image image (L "sunflower.jpg "); Image * pthumbnail = image. getthumbnailimage (50, 50, null, null ); // Display the thumbnail Graphics. drawimage (pthumbnail, 20, 20 ); // Do not forget to delete the thumbnail pointer after use Delete pthumbnail; Image rotation and stretching Image rotation and stretching are usually achieved by specifying the destpoints parameter in drawimage. destpoints contains data on points defined in the new coordinate system. Figure 7.18 illustrates how to define a coordinate system. We can see that the first point in destpoints is used to define the coordinate origin, and the second point is used to define the method of the X axis and the size of the image in the X direction, the third is the method used to define the Y axis and the size of the image in the Y direction. If the direction of the two axes in the new coordinate system defined by destpoints is not vertical, the image stretching effect can be achieved. The following code is an example of image rotation and stretching, as shown in result 7.19. Image image (L "sunflower.jpg "); Graphics. drawimage (& image, 10, 10 ); Point Points [] = {point (0, 0), point (image. getwidth (), 0 ), Point (0, image. getheight ())}; Matrix matrix (,); // defines a matrix of units. The coordinates are) Matrix. Rotate (30); // rotate 30 degrees clockwise Matrix. Scale (0.63, 0.6); // multiply the X and Y directions by the ratio of 0.63 and 0.6, respectively. Matrix. transformpoints (points, 3); // use this matrix to convert points Graphics. drawimage (& image, points, 3 ); Point newpoints [] = {point (450, 10), point (510, 60), point (350, 80 )}; Graphics. drawimage (& image, newpoints, 3 ); Of course, you can also directly use graphics: rotatetransform for image rotation, for example, the following code. However, after this setting, all the drawing results will be rotated in the future, and sometimes it may be inconvenient. Image image (L "sunflower.jpg "); Graphics. translatetransform (); // move the origin) Graphics. rotatetransform (30); // rotate 30 degrees clockwise Graphics. drawimage (& image, 0, 0 ); Adjust InterpolationAlgorithmQuality When the image is scaled, the image pixels need to be interpolated. Different interpolation algorithms have different effects. Graphics: setinterpolationmode allows us to use interpolation algorithms with different quality effects based on our own needs. Of course, the higher the quality, the longer the rendering time. The following code uses the interpolation algorithm mode with different quality effects. The result 7.20 is displayed. Graphics graphics (PDC-> m_hdc ); Image image (L "log.gif "); Uint width = image. getwidth (); Uint Height = image. getheight (); // No Scaling Graphics. drawimage (& image, 10, 10 ); // Use low-quality interpolation algorithms Graphics. setinterpolationmode (interpolationmodenearestneighbor ); Graphics. drawimage (& image, Rect (170, 30, (INT) (0.6 * width), (INT) (0.6 * Height ))); // Use a medium-quality Interpolation Algorithm Graphics. setinterpolationmode (interpolationmodehighqualitybilinear ); Graphics. drawimage (& image, Rect (270, 30, (INT) (0.6 * width), (INT) (0.6 * Height ))); // Use high-quality interpolation algorithms Graphics. setinterpolationmode (interpolationmodehighqualitybicubic ); Graphics. drawimage (& image, Rect (370, 30, (INT) (0.6 * width), (INT) (0.6 * Height ))); In fact, the image function is more than that, for example, conversion between files of different formats. However, these functions are basically the same as the new cimage function of MFC, but cimage is more in line withProgramSo the next section will focus on cimage usage methods and skills. |