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