Bitmap background transparency and setting bitmap transparency

Source: Internet
Author: User

How does a bitmap look like a PNG picture background transparent?

The key is the use of monochrome bitmap, monochrome bitmap, black for the foreground color (can represent any), white represents the picture background color

Implement Api:transparentblt to understand monochrome bitmaps

Function prototypes: BOOL TransparentBlt (HDC hdcdest, int nxorigindest, int nyorigindest, int nwidthdest,

int hheightdest, HDC hdcsrc, int nxoriginsrc, int nyoriginsrc, int nwidthsrc, int nheightsrc, UINT crtransparent);

Sample:transparentblt (DESHDC, 0, 0, N, N, srchdc, 0, 0, N/A, RGB (0,0,0xff));

+ =

From reason analysis: to attach a bitmap without background to another picture, it must end with srcpaint, or operation

It must be a black background picture, to keep the football pictures, the other side must leave a black football

, each of them for each other's "or operation" to preserve the black area, whereas "with the operation" respectively for each other to preserve the white area

The next step is to get two images:

First the local picture is loaded: hbitmap hbkbmp =:: LoadImage (AfxGetInstanceHandle (), path,null);

Hbitmap hfootballbmp =:: LoadImage (AfxGetInstanceHandle (), Path, NULL);

HDC HDC =:: GetDC (M_hwnd);

HDC HBKDC =:: CreateCompatibleDC (HDC);

HDC HFBDC =:: Createcomaptibledc (HDC);

Hbitmap HOLDBMP1 =:: SelectObject (HBKDC, hbkbmp);

Hbitmap HOLDBMP2 =:: SelectObject (HFBDC, hfootballbmp);

Omit this piece of code in the middle and put it in the back.

:: SelectObject

:: ReleaseDC (M_HWND,HDC);

Let's start with a monochrome mask bitmap.

Hbitmap createbitmap (int nwidth,int nheight, uint cplanes, uint cbitsperal,const VOID *lpvbits);

Hbitmap hmaskbmp =:: CreateBitmap (1, 1, NULL);

Specifies the soccer background color setbkcolor (HFBDC, RGB (0, 0, 0xff));

:: BitBlt (HMASKDC, 0, 0, Max, HFBDC, 0, 0, srccopy);

HDC HMASKDC =:: CreateCompatibleDC (HDC);

Step by step, we'll dig a hole in bkimage first.

:: BitBlt (HBKDC 0, 0, 0, 0, HMASKDC, Srcand);

Then we make another picture we need.

:: SetBkColor (HFBDC, RGB (255, 255, 255));

:: SetTextColor (HFBDC, RGB (0, 0, 0));

:: BitBlt (HFBDC, 0, 0, Max, HMASKDC, 0, 0, Srcand);

Like PS, set a picture of the foreground color is white, then the specified picture to the foreground will be white, this white is from the monochrome mask bitmap in the foreground black, the same background

Then there is nothing more,:: BitBlt (HBKDC, 0, 0, 0, 0, srcpaint);

As for setting bitmap transparency, just a function alphablend (...)

Here's an example:

Blendfunction blend;

memset (&blend, 0, sizeof (blend));

Blend. Blendop= Ac_src_over;

Blend. Sourceconstantalpha= M_nalpha; Transparency Max 255

AlphaBlend (Hdc,0,0,nwindth,nheight,hmaskdc,m_clientrect.left,

M_clientrect.top,m_clientrect.width (), M_clientrect.height (), blend);

Bitmap background transparency and setting bitmap transparency

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.