GDI + image processing based on VC. net

Source: Internet
Author: User
Tags bmp image

We know that in the past image processing, we often need to convert different formats based on the formats of different image files and their data storage structures. The display of an image file is based on the analysis of the file data structure and then reading the relevant image data. Currently, GDI + provides image and bitmap classes so that we can easily process images.

  Overview

GDI + supports most popular image file formats, such as BMP, GIF, JPEG, Tiff, and PNG. Next we will introduce these image files, and then describe the features supported by the image and bitmap classes.

1. Overview of image file formats

An image file is a computer disk file that depicts an image. There are dozens of file formats. This section only describes BMP, GIF, JPEG, Tiff, PNG, and other image file formats.

BMP file format

The BMP image file format is Microsoft's standard image format for its Windows environment. A Windows BMP bitmap is actually a bit array corresponding to the display pixel. It has two types, the other is the DIB bitmap (device-independent Bitmap ). A GDI bitmap contains a Windows data structure related to the Windows GDI module. The data structure is related to devices. Therefore, a bitmap is also called a device-Dependent Bitmap ). When the user program obtains the bitmap data information, the bitmap display method depends on the display card. Due to the device dependency of the GDI bitmap, when the bitmap is transmitted to another PC over the network, problems may occur.

DiB has many programming advantages over GDI bitmap. For example, it comes with color information, making it easier to manage the palette. In addition, any machine running Windows can process Dib, which is usually stored in the disk as A. BMP file or as a resource in the EXE or DLL file of the program.

GIF File Format

The GIF-Graphics Interchange Format was first set by CompuServe in June 15, 1987 and is mainly used for online transmission and storage of CompuServe network graphic data. GIF provides sufficient information and organizes the information well, allowing many different input/output devices to conveniently exchange images. It supports 24-bit color, implemented by a palette of up to 256 colors, the image size is up to 64 k x 64 K image points. GIF features LZW compression, multiple images, and staggered screen plotting.

JPEG file format

After five years of hard work, in March 1991, a draft proposal for iso cd 10918 was proposed: "digital compression coding for multiple gray-scale static images" (generally referred to as JPEG ). This is a compression standard for color and monochrome multi-gray or continuous tone static digital images. It includes lossless compression and lossy compression based on discrete cosine transformation and Huffman encoding. The former will not produce distortion, but the compression ratio is very small. When the latter algorithm compresses images, although there is a loss of information, the compression ratio can be very large. For example, compressing 20 ~ At 40 times, the human eyes basically do not see distortion.

JPEG image files are also in pixel format, but they are much more complex than BMP and other image files. Fortunately, the image of GDI + provides support for the JPEG file format, so that we can process the image in this format without having to know too much about the JPEG format.

TIFF File Format

Tiff (Tagged Image Format File) was first launched by Aldus in 1986. It supports all images from monochrome to 24-bit true color, in addition, modification and conversion between different platforms are very easy. Unlike other image file formats, the TIFF file contains a tag information area that defines the image data types, colors, and compression methods for file storage.

PNG file format

The 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. It is used to replace the GIF image file format, which is relatively simple and has strict patent restrictions. In addition, this image file format can even replace the complicated TIFF image file format to some extent. Its main features include: compression efficiency is usually higher than GIF, alpha channel is provided to control the transparency of the image, and Gamma Correction 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, grayscale image, and color index data image. JPEG only supports the first two image types. Although GIF can use the grayscale palette to compensate for the gray level of the image, it only supports the third image type in principle.

  Mage and bitmap class overview

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 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, 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 the quality of interpolation algorithms

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 with the programming habits of MFC programmers. Therefore, we will introduce the usage methods and skills of cimage in the next section.

 

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.