How to display transparent PNG on Windows Mobile

Source: Internet
Author: User
Tags transparent color

During the porting process, we found that. NET Compact framework does not support transparent images. PNG with transparent properties (including alpha channel) is no longer transparent after being displayed through graphics. drawimage. This is troublesome for hierarchical map display. For example. A satellite map with a place name is generally composed of two layers of images.

The two images are superimposed to form the final image.

Because. NET Compact framework does not support transparent images by default, the road map overwrites the satellite map that falls down. The original transparent color turns white. If there are other layers (such as paths), the road map will be overwritten.
After Google search, there are two ways to display transparent images on Windows Mobile.

Is through the iimagingfactory Interface

Using system; <br/> using system. drawing; <br/> using system. runtime. interopservices; <br/> namespace dotnetpocketstreet. drawing <br/> {<br/> Enum imagelockmode <br/> {<br/> imagelockmoderead = 0x0001, <br/> imagelockmodewrite = 0x0002, <br/> imagelockmodeuserinputbuf = 0x0004 <br/>}; <br/> // pulled from gdipluspixelformats. h In the Windows Mobile 5.0 Pocket pc sdk <br/> Public Enum pixelformatid: int <br/>{< br/> pixelformatindexed = 0x00010000, // indexes into a palette <br/> pixelformatgdi = 0x00020000, // is a GDI-supported format <br/> pixelformatalpha = 0x00040000, // has an Alpha component <br/> pixelformatpalpha = 0x00080000, // pre-Multiplied Alpha <br/> pixelformatextended = 0x00100000, // extended color 16 bits/channel <br/> pixelformatcanonical = 0x00200000, <br/> pixelformatundefined = 0, <br/> pixelformatdontcare = 0, <br/> pixelformat1bppindexed = (1 | (1 | pixelformatindexed | pixelformatgdi ), <br/> pixelformat4bppindexed = (2 | (4 | pixelformatindexed | pixelformatgdi ), <br/> pixelformat8bppindexed = (3 | (8 | pixelformatindexed | pixelformatgdi ), <br/> pixelformat16bpprgb555 = (5 | (16 | pixelformatgdi ), <br/> pixelformat16bpprgb565 = (6 | (16 | pixelformatgdi ), <br/> pixelformat16bppargb1555 = (7 | (16 | pixelformatalpha | pixelformatgdi ), <br/> pixelformat24bpprgb = (8 | (24 | pixelformatgdi ), <br/> pixelformat32bpprgb = (9 | (32 | pixelformatgdi ), <br/> pixelformat32bppargb = (10 | (32 | pixelformatalpha | pixelformatgdi | pixelformatcanonical ), <br/> pixelformat32bpppargb = (11 | (32 | pixelformatalpha | pixelformatpalpha | pixelformatgdi ), <br/> pixelformat48bpprgb = (12 | (48 | pixelformatextended ), <br/> pixelformat64bppargb = (13 | (64 | pixelformatalpha | pixelformatcanonical | pixelformatextended ), <br/> pixelformat64bpppargb = (14 | (64 | pixelformatalpha | pixelformatpalpha | pixelformatextended), <br/> pixelformatmax = 15 <br/>}< br/> // pulled from imaging. h In the Windows Mobile 5.0 Pocket pc sdk <br/> Public Enum bufferdisposalflag: int <br/>{< br/> bufferdisposalflagnone, <br/> bufferdisposalflagglobalfree, <br/> bufferdisposalflagcotaskmemfree, <br/> bufferdisposalflagunmapview <br/>}< br/> // pulled from imaging. h In the Windows Mobile 5.0 Pocket pc sdk <br/> Public Enum interpolationhint: int <br/>{< br/> interpolationhintdefault, <br/> interpolationhintnearestneighbor, <br/> interpolationhintbilinear, <br/> interpolationhintaveraging, <br/> interpolationhintbicubic <br/>}< br/> // pulled from gdiplusimaging. h In the Windows Mobile 5.0 Pocket pc sdk <br/> public struct bitmapdata <br/>{< br/> Public uint width; <br/> Public uint height; <br/> Public int stride; <br/> Public pixelformatid pixelformat; <br/> Public intptr scan0; <br/> Public intptr reserved; <br/>}< br/> // pulled from imaging. h In the Windows Mobile 5.0 Pocket pc sdk <br/> public struct imageinfo <br/>{< br/> Public uint guidpart1; // I am being lazy here, I don't care at this point about the rawdataformat guid <br/> Public uint guidpart2; // I am being lazy here, I don't care at this point about the rawdataformat guid <br/> Public uint guidpart3; // I am being lazy here, I don't care at this point about the rawdataformat guid <br/> Public uint guidpart4; // I am being lazy here, I don't care at this point about the rawdataformat guid <br/> Public pixelformatid pixelformat; <br/> Public uint width; <br/> Public uint height; <br/> Public uint tilewidth; <br/> Public uint tileheight; <br/> Public double xdpi; <br/> Public double ydpi; <br/> Public uint flags; <br/>}< br/> // pulled from imaging. h In the Windows Mobile 5.0 Pocket pc sdk <br/> [comimport, GUID ("327abda7-072b-11d3-9d7b-rjf81ef32e"), interfacetype (cominterfacetype. interfaceisiunknown)] <br/> [comvisible (true)] <br/> Public interface iimagingfactory <br/>{< br/> uint createimagefromstream (); // This Is A Place Holder, note the lack of arguments <br/> uint createimagefromfile (string filename, out inativeimage image ); <br/> // we need the specified alas attribute here to keep com InterOP from sending the buffer down as a safe array. <br/> uint createimagefrombuffer ([financialas (unmanagedtype. lparray)] Byte [] buffer, uint size, includisposalflag, out inativeimage); <br/> uint createnewbitmap (uint width, uint height, pixelformatid pixelformat, out ibitmapimage Bitmap ); <br/> uint createbitmapfromimage (inativeimage image, uint width, uint height, pixelformatid pixelformat, interpolationhint hints, out ibitmapimage Bitmap); <br/> uint createbitmapfrombuffer (); // This Is A Place Holder, note the lack of arguments <br/> uint createimagedecoder (); // This Is A place holder, note the lack of arguments <br/> uint createimageencodertostream (); // This Is A Place Holder, note the lack of arguments <br/> uint createimageencodertofile (); // This Is A Place Holder, note the lack of arguments <br/> uint getinstalleddecoders (); // This Is A place holder, note the lack of arguments <br/> uint getinstalledencoders (); // This Is A Place Holder, note the lack of arguments <br/> uint installimagecodec (); // This Is A Place Holder, note the lack of arguments <br/> uint uninstallimagecodec (); // This Is A place holder, note the lack of arguments <br/>}< br/> // pulled from imaging. h In the Windows Mobile 5.0 Pocket pc sdk <br/> [comimport, GUID ("327abda9-072b-11d3-9d7b-rjf81ef32e"), interfacetype (cominterfacetype. interfaceisiunknown)] <br/> [comvisible (true)] <br/> Public interface inativeimage <br/>{< br/> uint getphysicaldimension (out size ); <br/> uint getimageinfo (Out imageinfo info); <br/> uint setimageflags (uint flags); <br/> uint draw (intptr HDC, ref rectangle dstrect, intptr null); // "correct" Declaration: uint draw (intptr HDC, ref rectangle dstrect, ref rectangle srcrect); <br/> uint pushintosink (); // This Is A Place Holder, note the lack of arguments <br/> uint getthumbnail (uint thumbwidth, uint thumbheight, out inativeimage thumbimage ); <br/>}< br/> // pulled from imaging. h In the Windows Mobile 5.0 Pocket pc sdk <br/> [comimport, GUID ("327abdaa-072b-11d3-9d7b-rjf81ef32e"), interfacetype (cominterfacetype. interfaceisiunknown)] <br/> [comvisible (true)] <br/> Public interface ibitmapimage <br/>{< br/> uint getsize (out size ); <br/> uint getpixelformatid (Out pixelformatid pixelformat); <br/> uint lockbits (ref rectangle rect, uint flags, pixelformatid pixelformat, out bitmapdata lockedbitmapdata ); <br/> uint unlockbits (ref bitmapdata lockedbitmapdata); <br/> uint getpalette (); // This Is A place holder, note the lack of arguments <br/> uint setpalette (); // This Is A Place Holder, note the lack of arguments <br/>}< br/>}

the call method is as follows

<Textarea readonly name = "code" class = "CSHARP"> using (Graphics gxbuffer = graphics. fromimage (backbuffer) <br/>{< br/> // Since we NOP 'd onpaintbackground, take care of it here <br/> gxbuffer. clear (this. backcolor); <br/> // ask the image object from imaging to draw itself. <br/> intptr hdcdest = gxbuffer. gethdc (); <br/> rectangle dstrect = new rectangle (100,100,148,148); <br/> imagingresource. draw (hdcdest, ref dstrect, intptr. zero); <br/> gxbuffer. releasehdc (hdcdest); <br/> // ask the image object from imaging to draw itself. <br/>/* intptr */hdcdest = gxbuffer. gethdc (); // This is redundant, but keeps the necessary code together <br/>/* rectangle */dstrect = new rectangle (50, 70, 50 + 132, 70 + 132); <br/> imagingimage. draw (hdcdest, ref dstrect, intptr. zero); <br/> gxbuffer. releasehdc (hdcdest); <br/>}< br/> // put the final composed image on screen. <br/> E. graphics. drawimage (backbuffer, 0, 0); </textarea>

Reference http://msdn.microsoft.com/en-us/library/aa452202.aspx documentation

Another method is to use manged Code. For images that know the transparent color value in advance, such as the path in the map API, the background color is always 0xffe0e0e0.
You can use the following method:

 

 

Imageattributes _ imageattributes = new imageattributes (); <br/> color = color. fromargb (0xe0e0e0); <br/> _ imageattributes. setcolorkey (color, color); <br/> rectangle dstrect = <br/> New rectangle (X, Y, netimage. getwidth (), netimage. getheight (); <br/> gxbuffer. drawimage (netimage. _ bitmap, dstrect, 0, 0, <br/> netimage. getwidth (), <br/> netimage. getheight (), <br/> graphicsunit. pixel, _ imageattributes );

The final result is as follows:

 

 

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.