VC + + use CImage png to BMP picture transparent background processing

Source: Internet
Author: User
Tags transparent color

PNG format picture is to support transparent channel, BMP format picture is not transparent channel, so when the PNG format of the picture converted to BMP format, the PNG image for the transparent background needs to be special processing.

VC + + HBITMAP is supported transparent color, if HBITMAP is a PNG format picture handle, use Cimage:save or other way to save to the file, the transparent background will turn black.

The simplest way to solve this problem is with GDI +, libpng, and so on, using CImage to directly replace the transparent portions of the image data with white or other colors.

Advantages: Simple treatment method, high efficiency, just cimage;

Cons: produces slightly jagged edges at the corner of the picture;

I write the function, you can combine their own needs to decide whether to use:

12345678910111213141516171819202122 HBITMAPTransparentImage(HBITMAPhBitmap){    CImage Image;    Image.Attach(hBitmap);    intnPitch = Image.GetPitch( ) , nBPP = Image.GetBPP( );    LPBYTElpBits = reinterpret_castLPBYTE>( Image.GetBits( ) );    forintyPos = 0 ; yPos < Image.GetHeight( ) ; yPos ++ )    {        LPBYTElpBytes = lpBits + ( yPos * nPitch );        PDWORD lpLines = reinterpret_cast< PDWORD >( lpBytes );        forintxPos = 0 ; xPos < Image.GetWidth( ) ; xPos ++ )        {            if( nBPP == 32 && lpLines[ xPos ] >> 24 != 0x000000FF)            {                lpLines[ xPos ] |= 0xFFFFFFFF ;            }        }    }    returnImage.Detach();}

lplines[XPos] |= 0xFFFFFFFF is the color to be replaced, set to a custom color value.

Record, for the better of myself!

VC + + use CImage png to BMP picture transparent background processing

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.