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:

Hbitmap transparentimage (hbitmap hbitmap) {CImage image;image. Attach (HBITMAP); int npitch = Image.getpitch (), nbpp = IMAGE.GETBPP (); Lpbyte lpbits = reinterpret_cast< lpbyte> (image.getbits ()); for (int yPos = 0; YPos < Image.getheight (); yPo S + +) {Lpbyte lpbytes = lpbits + (YPos * npitch); Pdword lplines = reinterpret_cast< pdword > (lpbytes); for (int xPos = 0; XPos < Image.getwidth (); XPos + +) {I F (nbpp = = && lplines[XPos] >> + = 0x000000ff) {lplines[xPos] |= 0xFFFFFFFF;}}} return Image.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.