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.