Use drawimage and textbrush

Source: Internet
Author: User

In the process of using GDI +, you often need to paste images on the dialog box or control. Most of the time, we use the drawimage function, which is very useful. When the target area is larger than the image, the image can be stretched, but when the image is gradient, the color will change after it is stretched. This is a parameter that needs to be set when drawimage is used, for example:

 
Imageattributes IA; IA. setwrapmode (wrapmodetileflipxy); graphic. drawimage (pimg, grect,0,0, (Real) pimg-> getwidth (), (real) pimg-> getheight (), unitpixel, & IA );

For more information about wrapmodetileflipxy, see the description of msdn.

In addition to drawimage, GDI + also has a function fillrectangle that can fill a solid color in a rectangle. If you use a textbrush to fillrectangle, you can fill a rectangular area with an image, for example:

Crect RC (10,10,15,15); Texturebrush textbrush (image: fromfile (_ T (\ res \ picture.png), wrapmodetile); graphic. fillrectangle (& Textbrush, RC. Right-4, RC. Top,4,5);

However, when we use fillrectangle to paste images, the image may be misplaced because fillrectangle is not directly filled with the target rectangular area, it first fills the area of the current DC with an image, and then removes the rectangular area of the same size as the target area in the area of the DC to fill the target area, because the image is smaller than the area contained in the DC, when filling the area of the rectangle contained in the DC, It is tiled one by one, in this way, when the right side of the area contained in DC is taken from the rectangle area of the same size as the target area, the image style is uncontrollable, so the effect will be misplaced.

There are two ways to solve this problem. One is to set the DC drawing area to the same as the target DC. For example: (we usually use dual-buffer technology pasters so that the image will not flash)

Crect destrect ( 10 , 10 , 15 , 15  ); HDC hmemdc = : Createcompatibledc (HDC );  //  Create a compatible bitmap of the same size as the target region Hbitmap hbmp = : Createcompatiblebitmap (HDC, destrect. Width (), destrect. Height (); handle hold = : SelectObject (hmemdc, hbmp); graphics graphic (hmemdc); texturebrush textbrush (image: fromfile (_ T (\ res \ picture.png), wrapmodetile); graphic. fillrectangle ( & Textbrush, destrect. Left, RC. Top, 5 , 5 );: Bitblt (HDC, destrect. Left, destrect. Top, RC. Width (), RC. Height (), hmemdc,  0 , 0  , Srccopy);: SelectObject (hmemdc, hold);: deleteobject (hbmp);: deletedc (hmemdc ); 

However, sometimes our DC region cannot be the same size as the target region, because we may need to draw a large basemap, modify a small area above, and paste another image, in this case, you can use another method, that is, to translate coordinates, such:

 .................................... Crect dcrect (  10 , 10 , 20 , 20  ); Crect destrect (  15 ,15 , 20 , 20  ); Texturebrush textbrush (image: fromfile (_ T (\ res \ picture.png), wrapmodetile );  //  Moving coordinates Textbrush. translatetransform (real) (dcrect. Right- 5 ), (Real) (dcrect. Bottom- 5  ); Graphic. fillrectangle ( & Textbrush, destrect. Left, destrect. Top, 5 , 5  );.................. 

 

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.