How to use Windows Api AlphaBlend

Source: Internet
Author: User
Tags bmp image

Original link: http://www.cnblogs.com/sosopop/p/3412962.html

Early in the company developed a C + + client software, need to support windows2000, requirements simple and small, and do not want to introduce other interface library, graphics engine and other things, such as GDI + and so on, so for the art to do some of the interface, need to have alpha channel image rendering is very troublesome, The efficiency of your own writing is still somewhat low. So I found the AlphaBlend function from MSDN to do the alpha blending of the image, just at the beginning, thinking that this function can only handle the overall transparency of the entire image, not the transparency of each pixel of the 32-bit image, and then carefully study, The discovery can be done by configuring the following parameters:

Blendfunction _blendfunction = {ac_src_over, 0, 255, ac_src_alpha};

But the rendered color is always wrong. Search from the Internet can not find the answer, and then carefully studied the next MSDN, one of the words caught my attention premultiply, what is the pre-multiply alpha value. Let's take a look at the Alpha Blend formula:

Color = (SRCRGB * srcalpha) + (DESTRGB * (1-srcalpha))

WHERE (SRCRGB * srcalpha) This step is in the rendering, or before loading can be calculated, the image re-refresh rendering, you can directly use the calculated pre-multiply alpha value, instead of calculating this step multiplication, The rendering speed is thus added. Therefore, an image that contains an alpha channel normally needs special processing to render to DC using AlphaBlend.

How do you pre-multiply the image of an alpha image before rendering? One is to iterate through each pixel after loading the image and perform an alpha pre-multiplication operation. Another way is to have the artist directly output an alpha image that has been pre-multiplied. The first kind of programmer will, the second way, Maybe programmers and artists do not know how to do, here is the second way to talk about it,.

Start by loading a PNG with alpha information, or a PSD of your own design

Copy the layer to a new

Select the layer you just copied, press Ctrl+u, adjust the lightness to 100, and click OK to copy.

Create a new channel in the channel panel and paste the white Apple you just copied onto the new channel

Return to the Layers panel, remove the white apples from the layers panel, and create a new layer underneath the colored apple and fill it with black.

Save as BMP image, color depth Select 32 bits, so we do a pre-multiply alpha image.

This is a simple example code.

Http://pan.baidu.com/s/11j9OJ

How to use Windows Api AlphaBlend

Related Article

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.