在Windows Mobile 顯示透明 PNG的方法

來源:互聯網
上載者:User

同樣在移植的過程中,發現 .Net compact Framework 不支援透明映像。原本具有透明屬性的Png (含有 alpha通道),通過 Graphics.DrawImage 顯示之後,不再具有透明特性。這對於地圖分層顯示帶了麻煩。舉例來說。帶地名衛星地圖一般是由兩層圖片疊加而成。

兩個圖片疊加形成最後的圖片

當由於.Net Compact Framework預設不支援透明映像,兩幅圖疊加是 道路圖回徹底覆蓋掉下面的衛星圖。原來的透明色變成白色。 同樣如果再有其它圖層(比如路徑),又覆蓋掉道路圖。
經過Google 搜尋,有兩種方法可以實現在Windows mobile 上透明映像的顯示。

是通過IImagingFactory 介面

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 << <img class="wp-smiley" alt="8)" src="http://www.imobilebbs.com/wordpress/wp-includes/images/smilies/icon_cool.gif"> | PixelFormatIndexed | PixelFormatGDI),<br />PixelFormat4bppIndexed = (2 | ( 4 << <img class="wp-smiley" alt="8)" src="http://www.imobilebbs.com/wordpress/wp-includes/images/smilies/icon_cool.gif"> | PixelFormatIndexed | PixelFormatGDI),<br />PixelFormat8bppIndexed = (3 | ( 8 << <img class="wp-smiley" alt="8)" src="http://www.imobilebbs.com/wordpress/wp-includes/images/smilies/icon_cool.gif"> | PixelFormatIndexed | PixelFormatGDI),<br />PixelFormat16bppRGB555 = (5 | (16 << <img class="wp-smiley" alt="8)" src="http://www.imobilebbs.com/wordpress/wp-includes/images/smilies/icon_cool.gif"> | PixelFormatGDI),<br />PixelFormat16bppRGB565 = (6 | (16 << <img class="wp-smiley" alt="8)" src="http://www.imobilebbs.com/wordpress/wp-includes/images/smilies/icon_cool.gif"> | PixelFormatGDI),<br />PixelFormat16bppARGB1555 = (7 | (16 << <img class="wp-smiley" alt="8)" src="http://www.imobilebbs.com/wordpress/wp-includes/images/smilies/icon_cool.gif"> | PixelFormatAlpha | PixelFormatGDI),<br />PixelFormat24bppRGB = (8 | (24 << <img class="wp-smiley" alt="8)" src="http://www.imobilebbs.com/wordpress/wp-includes/images/smilies/icon_cool.gif"> | PixelFormatGDI),<br />PixelFormat32bppRGB = (9 | (32 << <img class="wp-smiley" alt="8)" src="http://www.imobilebbs.com/wordpress/wp-includes/images/smilies/icon_cool.gif"> | PixelFormatGDI),<br />PixelFormat32bppARGB = (10 | (32 << <img class="wp-smiley" alt="8)" src="http://www.imobilebbs.com/wordpress/wp-includes/images/smilies/icon_cool.gif"> | PixelFormatAlpha | PixelFormatGDI | PixelFormatCanonical),<br />PixelFormat32bppPARGB = (11 | (32 << <img class="wp-smiley" alt="8)" src="http://www.imobilebbs.com/wordpress/wp-includes/images/smilies/icon_cool.gif"> | PixelFormatAlpha | PixelFormatPAlpha | PixelFormatGDI),<br />PixelFormat48bppRGB = (12 | (48 << <img class="wp-smiley" alt="8)" src="http://www.imobilebbs.com/wordpress/wp-includes/images/smilies/icon_cool.gif"> | PixelFormatExtended),<br />PixelFormat64bppARGB = (13 | (64 << <img class="wp-smiley" alt="8)" src="http://www.imobilebbs.com/wordpress/wp-includes/images/smilies/icon_cool.gif"> | PixelFormatAlpha | PixelFormatCanonical | PixelFormatExtended),<br />PixelFormat64bppPARGB = (14 | (64 << <img class="wp-smiley" alt="8)" src="http://www.imobilebbs.com/wordpress/wp-includes/images/smilies/icon_cool.gif"> | 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-0000F81EF32E"), 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 MarshalAs attribute here to keep COM interop from sending the buffer down as a Safe Array.<br />uint CreateImageFromBuffer([MarshalAs(UnmanagedType.LPArray)] byte[] buffer, uint size, BufferDisposalFlag disposalFlag, out INativeImage image);<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-0000F81EF32E"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]<br />[ComVisible(true)]<br />public interface INativeImage<br />{<br />uint GetPhysicalDimension(out Size 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-0000F81EF32E"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]<br />[ComVisible(true)]<br />public interface IBitmapImage<br />{<br />uint GetSize(out Size 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 />}

調用方法如下

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);

文檔可參考 http://msdn.microsoft.com/en-us/library/aa452202.aspx

另外一種方法還是採用Manged code, 對於預Crowdsourced Security Testing道透明色值的映像,比如地圖API中的路徑,背景色總為0xFFE0E0E0
可以使用下面方法

 

 

ImageAttributes _imageAttributes = new ImageAttributes();<br />Color 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);

最終結果如

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.