GDI + settings and wonderful instances under vc6.0

Source: Internet
Author: User

1. download the gdiplus file (GDI + for vc6.0 SDK) and copy the files in the uplodes and Lib files to the uplodes and Lib folders in the VC directory. place the DLL file in system32. if it already exists, you do not need to replace it.

: Http://www.codeguru.com/code/legacy/gdi/GDIPlus.zip

2. Complete the initialization in the project where you are going to use GDI +:

Add in stdafx. h

# Define ulong_ptr unsigned long

# Include using namespace gdiplus;

# Include "gdiplus. H"

3. Add the following content to the header file

Ulong_ptr m_gdiplustoken;

// It must be added in the form of member variables. The author did not clarify this. In fact, this is the basic process of MFC.

4. In bool c × app: initinstance (), add

Gdiplusstartupinput m_gdiplusstartupinput;

Gdiplusstartup (& m_gdiplustoken, & m_gdiplusstartupinput, null );

GDI + is based on COM and must be initialized during use

 

5. Add int cxapp: exitinstance () (This method needs to be added to classwizard. Note that select cxapp in classname and exitinstance in messages list box)

Gdiplusshutdown (m_gdipluw.en );

6. Add gdiplus. lib to project-> stting-> link-> Object/libary. At this time, GDI + is set successfully.

 

 

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 );

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.