Use GDI + for Alpha Mixing

Source: Internet
Author: User

Mathematical formula of Alpha Blend:
Displaycolor = sourcecolor × alpha/255 + backgroundcolor × (255-alpha)/255

 

Void alphablend_1 () <br/>{< br/> Bitmap bitmap (L "demo.jpg"); <br/> int iwidth = bitmap. getwidth (); <br/> int iheight = bitmap. getheight (); </P> <p> color; <br/> color colortemp; </P> <p> for (INT irow = 0; irow <iheight; irow ++) <br/>{< br/> for (INT icolumn = 0; icolumn <iwidth; icolumn ++) <br/>{< br/> bitmap. getpixel (icolumn, irow, & color); </P> <p> colortemp. setvalue (color. makeargb (<br/> (byte) (128), // indicates the degree of Alpha mixing (which can be understood as transparency) <br/> color. getred (), <br/> color. getgreen (), <br/> color. getblue () <br/>); </P> <p> bitmap. setpixel (icolumn, irow, colortemp); <br/>}</P> <p> // at this time, bitmap is a translucent image <br/>}

 

Void alphablend_2 (HDC) <br/>{< br/> colormatrix ={< br/> 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, <br/> 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, <br/> 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, <br/> 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, // The value of column 4th in this row (here it is 0.5, valid range []) indicates the degree of Alpha mixing (which can be understood as transparency) <br/> 0.0f, 0.0f, 0.0f, 0.0f, 1.0f <br/>}; </P> <p> imageattributes imageatt; <br/> imageatt. setcolormatrix (& colormatrix, colormatrixflagsdefault, coloradjusttypebitmap); </P> <p> Bitmap bitmap (L "demo.jpg"); <br/> int iwidth = bitmap. getwidth (); <br/> int iheight = bitmap. getheight (); </P> <p> graphics (HDC); </P> <p> // map bitmap in translucent form <br/> graphics. drawimage (<br/> & bitmap, <br/> rect (10, 10, iwidth, iheight), <br/> 0, <br/> 0, <br/> iwidth, <br/> iheight, <br/> unitpixel, <br/> & imageatt <br/>); <br/>}

 

Note:

In fact, the most essential part of Alpha mixing is the mathematical formula given at the beginning of the article, even if there is no such function provided by GDI +, you can also take the pixels of the two images to be alpha-mixed one by one, calculate the result pixels according to the formula, and then combine them into a picture.

Of course, there are convenient tools to use. To borrow an ancient saying, the saying is "a gentleman is born like a different person, and good or false is also a thing ". Finally, I think that tools should be used, but it is better to know why, understand its specific principles, and even know why.

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.