Automatic gradient when drawing images using the GDI + drawimage Function

Source: Internet
Author: User

When drawing an image, the drawimage function will automatically increase the gradient if it is stretched. The detailed description is as follows.

Suppose we want to make a progress bar and use our own PNG format image as the background and foreground for drawing. At this time, we must stretch the image, first, we need to show different progress by stretching the amount horizontally, and second, in order to save the volume of resource files, we only need to have a few pixels (or even one pixel) in the horizontal direction of the image ). We use the following code to draw.

 

Graphics GFX (DC. getsafehdc ());
GFX. setinterpolationmode (interpolationmodenearestneighbor );
Rect rcdes (0, 0,100, 20 );
GFX. drawimage (m_pimage, rcdes, 0, 0, m_pimage-> getwidth (), m_pimage-> getheight (), unitpixel );

 

We will find that the output is different from what we imagined. The resource image is not simply scaled horizontally and filled with the target area, but automatically added with the gradient effect. When the actual size of the image is smaller than the size of the target area, the GDI + stretch image will add a gradient effect by default. (I have to say that GDI + is too powerful !) But now we don't need this annoying effect. What should we do?
You may soon think of a replacement product, that is, using texturebrush. The Code is as follows.

 

Rect rcimage (0, 0, m_pimage-> getwidth (), m_pimage-> getheight ());
Texturebrush brush (m_pimage, wrapmodetileflipx, rcimage );
GFX. fillrectangle (& brush, rcdes );

 

Texturebrush uses an image as the output background of the brush. With the paint brush, We can correctly output the desired effect. Some may ask: Is it possible that drawimage cannot complete the functions we want? If you carefully read the above Code, you should note that texturebrush has a construction parameter wrapmode, which sets the fill pattern for image stretching. wrapmodetileflipx indicates symmetric padding in the horizontal direction, in this way, there will be no gradient stretching in the horizontal direction. (You can study several other models on your own)

In fact, the drawimage function also has such a parameter, so we only need to use the following code to draw it:

 

 

 

Imageattributes imgatt;
Imgatt. setwrapmode (wrapmodetileflipxy );
GFX. drawimage (m_pimage, rcdes, 0, 0, m_pimage-> getwidth (), m_pimage-> getheight (),

Unitpixel, & imgatt );

 

OK. Now we can use the drawimage function to draw the desired effect. In order to encapsulate more comprehensive and powerful plotting functions, the GDI + function provides many heavy-duty functions with diverse parameters. during use, we can easily fall into complicated parameter setting traps. To make full use of the powerful functions of GDI +, it is very important to be skilled in application and experience accumulation.

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.