Transparent PNG image painting solution on Windows Mobile

Source: Internet
Author: User

Cocreateinstance creates iimagingfactory, then uses createimagefromfile to create the iimage interface, and finally draws the iimage to DC using the draw interface. But the problem is that the draw has not set the transparent attribute? How should I perform this operation? I saw a solution on the Internet. This is to convert the PNG image into a 32-Bit Bitmap and then transparently draw it to the DC. Here is a piece of code that has been successfully debugged:

View plaincopy to clipboardprint?
Hbitmap loadpngimage2 (HDC, lpctstr filename)
{
Iimagingfactory * pimagefactory = 0;
Iimage * pimage = 0;
Imageinfo;
Coinitializeex (0, coinit_multithreaded );
Hbitmap = 0;
Lpbyte;
If (succeeded (cocreateinstance (clsid_imagingfactory, 0, clsctx_inproc_server, iid_iimagingfactory, (void **) & pimagefactory )))
{
If (succeeded (pimagefactory-> createimagefromfile (filename, & pimage) & succeeded (pimage-> getimageinfo (& imageinfo )))
{
Hdc bmp Dc = createcompatibledc (HDC );
// Lpbyte;
Bitmapinfo * pbinfo;
Pbinfo = (bitmapinfo *) calloc (1, sizeof (bitmapinfo) + 4 * sizeof (INT ));
If (! Pbinfo)
Return false;
Pbinfo-> bmiheader. bisize = sizeof (bitmapinfoheader );
Pbinfo-> bmiheader. biwidth = imageinfo. width;
Pbinfo-> bmiheader. biheight = imageinfo. height;
Pbinfo-> bmiheader. biplanes = 1;
Pbinfo-> bmiheader. bibitcount = 32;
Pbinfo-> bmiheader. bicompression = bi_alphabitfields;
Pbinfo-> bmiheader. bisizeimage = 0;
Pbinfo-> bmiheader. bixpelspermeter = 11811;
Pbinfo-> bmiheader. biypelspermeter = 11811;
Pbinfo-> bmiheader. biclrused = 0;
Pbinfo-> bmiheader. biclrimportant = 0;
Int * pmask = (int *) & (pbinfo-> bmicolors [0]);
* Pmask ++ = 0x00ff0000;
* Pmask ++ = 0x0000ff00;
* Pmask ++ = 0x000000ff;
* Pmask ++ = 0xff000000;
Hbitmap = createdibsection (null, pbinfo, dib_rgb_colors, (void **) & lpbyte, null, 0 );
Free (pbinfo );
If (! Hbitmap |! Lpbyte)
Return false;
Rect = {0, 0, imageinfo. Width, imageinfo. Height };
Ibitmapimage * pbitmapimage;
Bitmapdata;
Bitmapdata. width = imageinfo. width;
Bitmapdata. Height = imageinfo. height;
Bitmapdata. pixelformat = imageinfo. pixelformat;
Pbitmapimage = NULL;
Pimagefactory-> createbitmapfromimage (pimage, imageinfo. Width, imageinfo. Height, pixfmt_32bpp_argb,
Interpolationhintdefault, & pbitmapimage );
Pbitmapimage-> lockbits (& rect, imagelockmoderead, pixfmt_32bpp_argb, & bitmapdata );
// Transferring the pixels
Memcpy (lpbyte, bitmapdata. scan0, imageinfo. Width * imageinfo. Height * 4 );
Pbitmapimage-> unlockbits (& bitmapdata );
Pbitmapimage-> release ();
Pimage-> release ();
Deletedc (bmp dc );
}
Pimagefactory-> release ();
}
Couninitialize ();
// Processthepixelswithalphachannel here
// Vertical flip and processthepixelswithalphachannel here
For (uint y = 0; y <imageinfo. Height/2; y ++)
{
Byte * ppixel = (byte *) lpbyte + imageinfo. Width * 4 * Y;
Byte * pdstpixel = (byte *) lpbyte + imageinfo. Width * 4 * (imageinfo. Height-y-1 );
For (uint x = 0; x <imageinfo. width; X ++)
{
Ppixel [0] = ppixel [0] * ppixel [3]/255;
Ppixel [1] = ppixel [1] * ppixel [3]/255;
Ppixel [2] = ppixel [2] * ppixel [3]/255;
Pdstpixel [0] = pdstpixel [0] * pdstpixel [3]/255;
Pdstpixel [1] = pdstpixel [1] * pdstpixel [3]/255;
Pdstpixel [2] = pdstpixel [2] * pdstpixel [3]/255;
Int * porigin = (int *) ppixel;
Int * pdst = (int *) pdstpixel;
Int temp = * porigin;
* Porigin = * pdst;
* Pdst = temp;
Ppixel + = 4;
Pdstpixel + = 4;
}
}
Return hbitmap;
}
Hbitmap loadpngimage2 (HDC, lpctstr filename)
{
Iimagingfactory * pimagefactory = 0;
Iimage * pimage = 0;
Imageinfo;
Coinitializeex (0, coinit_multithreaded );
Hbitmap = 0;
Lpbyte;
If (succeeded (cocreateinstance (clsid_imagingfactory, 0, clsctx_inproc_server, iid_iimagingfactory, (void **) & pimagefactory )))
{
If (succeeded (pimagefactory-> createimagefromfile (filename, & pimage) & succeeded (pimage-> getimageinfo (& imageinfo )))
{
Hdc bmp Dc = createcompatibledc (HDC );
// Lpbyte;
Bitmapinfo * pbinfo;
Pbinfo = (bitmapinfo *) calloc (1, sizeof (bitmapinfo) + 4 * sizeof (INT ));
If (! Pbinfo)
Return false;
Pbinfo-> bmiheader. bisize = sizeof (bitmapinfoheader );
Pbinfo-> bmiheader. biwidth = imageinfo. width;
Pbinfo-> bmiheader. biheight = imageinfo. height;
Pbinfo-> bmiheader. biplanes = 1;
Pbinfo-> bmiheader. bibitcount = 32;
Pbinfo-> bmiheader. bicompression = bi_alphabitfields;
Pbinfo-> bmiheader. bisizeimage = 0;
Pbinfo-> bmiheader. bixpelspermeter = 11811;
Pbinfo-> bmiheader. biypelspermeter = 11811;
Pbinfo-> bmiheader. biclrused = 0;
Pbinfo-> bmiheader. biclrimportant = 0;
Int * pmask = (int *) & (pbinfo-> bmicolors [0]);
* Pmask ++ = 0x00ff0000;
* Pmask ++ = 0x0000ff00;
* Pmask ++ = 0x000000ff;
* Pmask ++ = 0xff000000;
Hbitmap = createdibsection (null, pbinfo, dib_rgb_colors, (void **) & lpbyte, null, 0 );
Free (pbinfo );
If (! Hbitmap |! Lpbyte)
Return false;
Rect = {0, 0, imageinfo. Width, imageinfo. Height };
Ibitmapimage * pbitmapimage;
Bitmapdata;
Bitmapdata. width = imageinfo. width;
Bitmapdata. Height = imageinfo. height;
Bitmapdata. pixelformat = imageinfo. pixelformat;
Pbitmapimage = NULL;
Pimagefactory-> createbitmapfromimage (pimage, imageinfo. Width, imageinfo. Height, pixfmt_32bpp_argb,
Interpolationhintdefault, & pbitmapimage );
Pbitmapimage-> lockbits (& rect, imagelockmoderead, pixfmt_32bpp_argb, & bitmapdata );
// Transferring the pixels
Memcpy (lpbyte, bitmapdata. scan0, imageinfo. Width * imageinfo. Height * 4 );
Pbitmapimage-> unlockbits (& bitmapdata );
Pbitmapimage-> release ();
Pimage-> release ();
Deletedc (bmp dc );
}
Pimagefactory-> release ();
}
Couninitialize ();
// Processthepixelswithalphachannel here
// Vertical flip and processthepixelswithalphachannel here
For (uint y = 0; y <imageinfo. Height/2; y ++)
{
Byte * ppixel = (byte *) lpbyte + imageinfo. Width * 4 * Y;
Byte * pdstpixel = (byte *) lpbyte + imageinfo. Width * 4 * (imageinfo. Height-y-1 );
For (uint x = 0; x <imageinfo. width; X ++)
{
Ppixel [0] = ppixel [0] * ppixel [3]/255;
Ppixel [1] = ppixel [1] * ppixel [3]/255;
Ppixel [2] = ppixel [2] * ppixel [3]/255;
Pdstpixel [0] = pdstpixel [0] * pdstpixel [3]/255;
Pdstpixel [1] = pdstpixel [1] * pdstpixel [3]/255;
Pdstpixel [2] = pdstpixel [2] * pdstpixel [3]/255;
Int * porigin = (int *) ppixel;
Int * pdst = (int *) pdstpixel;
Int temp = * porigin;
* Porigin = * pdst;
* Pdst = temp;
Ppixel + = 4;
Pdstpixel + = 4;
}
}
Return hbitmap;
}
 

Similar source code can be found in a post on the msdn forum. but not very complete and correct. some corrections are made here. skip the previous processes, but looking at the for loop at the end, we actually made a vertical 180-degree flip. this means that the interface memcpy used to copy the previous image pixel information is actually flipped, which is reflected in many camera frames.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/hhygcy/archive/2009/04/29/4138195.aspx

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.