overview
GDI+ supports most popular image file formats, such as BMP, GIF, JPEG, TIFF and PNG. Let's introduce these Image files first, and then explain the features supported by the image and Bitmap classes.
1. Introduction to image file formats
An image file is a computer disk file that depicts an image, and there are dozens of file formats. Only the image file formats such as BMP, GIF, JPEG, TIFF and PNG are introduced here.
BMP file format
BMP image file format is a standard image format set by Microsoft for its Windows environment. A BMP bitmap of Windows is actually a bit array corresponding to display pixels, which has two types: one is called GDI bitmap, and the other is DIB bitmap (Device-Independent Bitmap). GDI bitmap contains a Windows data structure related to GDI module of Windows, which is related to devices, so it is also called DDB bitmap. When the user's program obtains bitmap data information, its bitmap display mode depends on the display card. Because of the device dependence of GDI bitmap, when the bitmap is transmitted to another PC through the network, problems are likely to occur.
DIB has many programming advantages over GDI bitmap, such as its own color information, which makes palette management easier. And any machine running Windows can handle DIB, and it is usually saved in the form of a file with a suffix of. BMP on disk or as a resource in an EXE or DLL file of a program.
GIF file format
GIF--Graphics Interchange Format was first formulated by CompuServe company on June 15th, 1987, which is mainly used for online transmission and storage of CompuServe network graphics data. GIF provides enough information and organizes it well, so that many different input and output devices can exchange images conveniently. GIF supports 24-bit color and is implemented by a palette of up to 256 colors. The size of an image is up to 64K x 64K pixels. GIF is characterized by LZW compression, multi-image and interlaced screen drawing.
JPEG file format
JPEG (Joint Photographic Experts Group), which was jointly established by the International Organization for Standardization (ISO) and CCITT, put forward the draft proposal of ISO CD 1918 in March 1991: "Digital compression coding of multi-gray still images" (usually referred to as JPEG standard for short). This is a compression standard suitable for color and monochrome multi-gray or continuous-tone still digital images. It includes lossless compression and lossy compression based on discrete cosine transform and Huffman coding. The former will not produce distortion, but the compression ratio is very small; When the latter algorithm is used for image compression, although the information is lost, the compression ratio can be very large. For example, when compressed by 2~4 times, the human eye can hardly see the distortion.
JPEG image file is also a file format in pixel format, but it is much more complicated than BMP and other image files. Fortunately, GDI+' s Image provides support for JPEG file format, so that we can process images in this format without knowing much about JPEG format.
TIFF file format
TIFF(Tagged Image Format File) was first introduced by Aldus in 1986. It can support any image from monochrome to 24-bit true color, and it is very easy to modify and convert between different platforms. Different from other image file formats, TIFF file has a tag information area to define the image data type, color and compression method stored in the file.
PNG file format
PNG(Portable Network Graphic) file format was proposed and designed by Thomas Boutell, Tom Lane and others. It is an image file format designed to adapt to network data transmission, and is used to replace GIF image file format with simple format and strict patent restrictions. Moreover, this image file format can even replace the TIFF image file format with complex format to some extent. Its main features are: compression efficiency is usually higher than GIF, Alpha channel is provided to control the transparency of the image, and Gamma correction mechanism is supported to adjust the brightness of the image.
It should be noted that the PNG file format supports three main image types: true color image, gray-scale image and color index data image. JPEG only supports the first two image types, while GIF can compensate the gray level of the image by using the gray palette, but in principle it only supports the third image type.
overview of mage and Bitmap classes
the Image class of GDI+encapsulates the functions of importing, format converting and simple processing BMP, GIF, JPEG, PNG, TIFF, WMF(Windows metafile) and EMF (enhanced WMF) image files. Bitmap is an Image class inherited from image class, which encapsulates the common functions of Windows bitmap operation. For example, Bitmap::SetPixel and Bitmap::GetPixel are used to read and write pixels on bitmaps respectively, which can provide a possibility for image softening and sharpening.
3. drawimage method
DrawImage is the core method of GDI+' s Graphics class to display images, and it has many overloaded functions. Commonly used general overloaded functions are:
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);
where (x,y) is used to specify the position where the image is displayed, which corresponds to the upper left corner of the image. Rect is used to specify the rectangular area filled by the image, and destPoints and count are used to specify the vertices and the number of vertices of a polygon respectively. If the count is 3, it means that the polygon is a parallelogram, and the other vertex is automatically given by the system. At this time, the data in destPoints correspond to the vertex coordinates of the upper left corner, the upper right corner and the lower left corner of the source image in turn. Srcx, srcy, srcwidth and srcheight are used to specify the location and size of the source image to be displayed, and srcUnit is used to specify the unit used. By default, PageUnitPixel is used, that is, pixels are used as the unit of measurement.
calling and displaying Image files
It is very easy to call and display image files in GDI+. Generally, an image file is called in through image or Bitmap to construct an object, and then the Graphics::DrawImage method is called to display all or part of the image at the specified position. For example, the following code:
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, 1,1);
Rect rect(13, 1, image.GetWidth(), image.GetHeight());
graphics.DrawImage(& image, rect);
}
The result is shown in Figure 7.17. From the figure, we can see that the results of two DrawImage are different and should be the same. What's going on? It turns out that DrawImage will automatically zoom according to the resolution of the device when the display area size is not specified, resulting in different display results.
Of course, you can also use the Bitmap class to call in an image file to construct a Bitmap object, and the result is the same. For example, the above code can be changed to:
Bitmap bmp(L"sunflower.jpg ");
graphics.DrawImage(& bmp, 1,1);
Rect rect(13, 1, bmp.GetWidth(), bmp.GetHeight());
graphics.DrawImage(& bmp, rect);
it should be noted that Image also provides GetThumbnailImage method to get a pointer to a thumbnail, which can be displayed after calling DrawImage, 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(5, 5, NULL, NULL);
// display the thumbnail
graphics.drawimage (pthumbmail, 2,2);
// Don't forget to delete the thumbnail pointer
Delete pthumbmail;
image rotation and stretching
image rotation and stretching are usually realized by specifying the destPoints parameter in DrawImage, which contains the data of points defined in the new coordinate system. Figure 7.18 illustrates the method of coordinate system definition.
as can be seen from the figure, the first point in destPoints is used to define the origin of coordinates, the second point is used to define the method of X axis and the size of image in X direction, and the third point is used to define the method of Y axis and the size of image in Y direction. If the directions of the two axes in the new coordinate system defined by destPoints are not perpendicular, the effect of image stretching can be achieved.
The following code is an example of image rotation and stretching, and the result is shown in Figure 7.19.
Image image(L"sunflower.jpg");
graphics.DrawImage(& image, 1,1);
Point points[] = { Point(, ), Point(image.GetWidth(), ),
Point(, image.GetHeight())};
Matrix matrix(1,,,1,23,1); //define a identity matrix whose coordinate origin is (23,1)
matrix.Rotate(3); //rotate 3 degrees clockwise
matrix.Scale(.63,.6); // X and y direction are multiplied by scale factors of .63 and .6 respectively
matrix.transform points (points, 3); //use this matrix to transform points
graphics.drawimage (&; image, points, 3);
Point newpoints[] = {Point(45, 1), Point(51, 6), Point(35, 8)};
graphics.DrawImage(& image, newpoints, 3);
Of course, you can also use Graphics::RotateTransform directly for image rotation, such as the following code. However, after this setting, all the drawing results will be rotated in the future, which may sometimes be inconvenient.
Image image(L"sunflower.jpg");
graphics.TranslateTransf