The PNG texture class is supported under wince. For work projects, PNG images must be used in wince application development, mainly for translucent effect. This was written at the beginning of last year. Recently, I plan to write several blog posts on WinCE to implement launcher similar to Android/iPhone, mainly for sliding, icon switching, and Icon stratified classification. PNG textures are required. This is an earlier version. Both decoding and Alpha use Microsoft's iimage library.
Alpha hybrid mainly uses Microsoft's alphablend (...) interface. Later I will write an article about Alpha hybridAlgorithmBecause Microsoft's alphablend speed is not very high, it will be faster to use its own Alpha algorithm.
For more information, see the header file interfaces. The class file implementation also has the necessary annotations. If you have any questions, leave a message for your discussion.
/* **************************************** ******* 2012, apical. CO ., ltd. edited by owlclass name: cpngbitblt description: ************************************* *********** */ # Pragma Once # Include " ./Ctrlconfig. h " Class Cpngbitblt { Public : Cpngbitblt ( Void ); ~ Cpngbitblt ( Void ); Public : /* Function: Load PNG image filename: PNG Image path hbit: returned PNG Image handle */ Bool loadpngimage (lpctstr filename, hbitmap * Hbit ); /* Function: paste the PNG image to the DC. PDC: Target DC psrdc: Image dc drc: Target map area SRC: source Image Area Alpha: transparency (0 ~ 255) */ Void Biltpng (CDC * PDC, CDC * psrdc, crect DRC, crect SRC, Int Alpha ); /* Function: paste the PNG image to the DC. PDC: Target DC psrdc: Image dc x: Target X coordinate Y: Target y coordinate width: Target Image Width heigth: Target Image Height ALPHA: transparency (0 ~ 255) */ Void Biltpng (CDC * PDC, CDC * psrdc, Int X, Int Y, Int Width, Int Height, Int Alpha ); /* **************************************** * **************************** Function Name: drawimage: loads an image from a file and draws it to the DC. parameters: [in] HDC: Target DC [in] iconpath: Path of the image [in] rect: returned value: no description: supports mainstream image types such as BMP, JPG, PNG, and GIF, iimage supports image decoding ********************************** *********************************** */ Void Drawimage2dc (HDC, lpcwstr iconpath, rect );};
# Include " Stdafx. h " # Include " Pngbitblt. h " # Include <Imaging. h> # Include <Initguid. h> # Include # Include <Wingdi. h> # Pragma Comment (Lib, "imaging. lib ") # Pragma Comment (Lib, "UUID. lib ") Cpngbitblt: cpngbitblt ( Void ) {} Cpngbitblt :: ~ Cpngbitblt ( Void ) {} Bool cpngbitblt: loadpngimage (lpctstr filename, hbitmap * Hbit) {iimagingfactory * Pimagefactory = 0 ; Iimage * Pimage = 0 ; Imageinfo; coinitializeex ( 0 , Coinit_multithreaded); hbitmap = 0 ; Lpbyte;
// Initialize the com Library 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. 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 ;}} * Hbit = Hbitmap; If (! Hbit ){ Return False ;} Return True ;} Void Cpngbitblt: biltpng (CDC * PDC, CDC * psrdc, crect DRC, crect SRC, Int Alpha) {blendfunction = { 0 }; Blendfunction. alphaformat = Ac_src_alpha; blendfunction. blendflags = 0 ; Blendfunction. blendop = Ac_src_over; blendfunction. sourceconstantalpha = Alpha; alphablend (PDC -> M_hdc, DRC. Left, DRC. Top, DRC. Width (), DRC. Height (), psrdc->M_hdc, SRC. Left, SRC. Top, SRC. Width (), SRC. Height (), blendfunction );} Void Cpngbitblt: biltpng (CDC * PDC, CDC * psrdc, Int X, Int Y, Int Width, Int Height, Int Alpha) {crect DRC = Crect (X, Y, x + width, Y + Height); crect SRC = Crect ( 0 , 0 , Width, height );
// Initialize the blendfunction struct, mainly including the Alpha hybrid type and related parameters.Blendfunction = { 0 }; Blendfunction. alphaformat = Ac_src_alpha; blendfunction. blendflags = 0 ; Blendfunction. blendop = Ac_src_over; blendfunction. sourceconstantalpha = Alpha; alphablend (PDC -> M_hdc, DRC. Left, DRC. Top, DRC. Width (), DRC. Height (), psrdc-> M_hdc, SRC. Left, SRC. Top, SRC. Width (), SRC. Height (), blendfunction );} Void Cpngbitblt: drawimage2dc (HDC, lpcwstr iconpath, rect) {iimage * M_pimage = NULL; iimagingfactory * M_pimagingfactory = NULL; hresult hr; // COM Initialization If (Failed (hR = Coinitializeex (null, coinit_multithreaded ))){ Goto End ;} // Create a com instance If (Failed (hR = cocreateinstance (clsid_imagingfactory, null, clsctx_inproc_server, iid_iimagingfactory ,( Void **)& M_pimagingfactory ))){ Goto End ;} // Create an image from a file If (Failed (hR = m_pimagingfactory-> createimagefromfile (iconpath ,& M_pimage ))){ Goto End ;} // Draw Images If (Failed (hR = m_pimage-> draw (HDC ,& Rect, null ))){ Goto End;} end: // Release resources If (M_pimage! = Null) {m_pimage -> Release (); m_pimage = NULL ;} If (M_pimagingfactory! = Null) {m_pimagingfactory ->Release (); m_pimagingfactory = NULL;} couninitialize ();}
If you have any questions, you can leave a message.
If you need to reprint a friend, please attach the original address:Http://www.cnblogs.com/mythou/archive/2013/06/13/3133606.html