热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在WindowsMobile显示透明PNG的方法

同样在移植的过程中,发现.NetcompactFramework不支持透明图像。原本具有透明属性的Png(含有alpha通道),通过Gra

同样在移植的过程中,发现 .Net compact Framework 不支持透明图像。原本具有透明属性的Png (含有 alpha通道),通过 Graphics.DrawImage 显示之后,不再具有透明特性。这对于地图分层显示带了麻烦。举例来说。带地名卫星地图一般是由两层图片叠加而成。

两个图片叠加形成最后的图片

当由于.Net Compact Framework缺省不支持透明图像,两幅图叠加是 道路图回彻底覆盖掉下面的卫星图。原来的透明色变成白色。 同样如果再有其它图层(比如路径),又覆盖掉道路图。
经过Google 搜索,有两种方法可以实现在Windows mobile 上透明图像的显示。

是通过IImagingFactory 接口

using System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace DotNetPocketStreet.Drawing
{
enum ImageLockMode
{
ImageLockModeRead = 0x0001,
ImageLockModeWrite = 0x0002,
ImageLockModeUserInputBuf = 0x0004
};
// Pulled from gdipluspixelformats.h in the Windows Mobile 5.0 Pocket PC SDK
public enum PixelFormatID : int
{
PixelFormatIndexed = 0x00010000, // Indexes into a palette
PixelFormatGDI = 0x00020000, // Is a GDI-supported format
PixelFormatAlpha = 0x00040000, // Has an alpha component
PixelFormatPAlpha = 0x00080000, // Pre-multiplied alpha
PixelFormatExtended = 0x00100000, // Extended color 16 bits/channel
PixelFormatCanonical = 0x00200000,
PixelFormatUndefined = 0,
PixelFormatDontCare = 0,
PixelFormat1bppIndexed &#61; (1 | ( 1 << | PixelFormatIndexed | PixelFormatGDI),
PixelFormat4bppIndexed &#61; (2 | ( 4 << | PixelFormatIndexed | PixelFormatGDI),
PixelFormat8bppIndexed &#61; (3 | ( 8 << | PixelFormatIndexed | PixelFormatGDI),
PixelFormat16bppRGB555 &#61; (5 | (16 << | PixelFormatGDI),
PixelFormat16bppRGB565 &#61; (6 | (16 << | PixelFormatGDI),
PixelFormat16bppARGB1555 &#61; (7 | (16 << | PixelFormatAlpha | PixelFormatGDI),
PixelFormat24bppRGB &#61; (8 | (24 << | PixelFormatGDI),
PixelFormat32bppRGB &#61; (9 | (32 << | PixelFormatGDI),
PixelFormat32bppARGB &#61; (10 | (32 << | PixelFormatAlpha | PixelFormatGDI | PixelFormatCanonical),
PixelFormat32bppPARGB &#61; (11 | (32 << | PixelFormatAlpha | PixelFormatPAlpha | PixelFormatGDI),
PixelFormat48bppRGB &#61; (12 | (48 << | PixelFormatExtended),
PixelFormat64bppARGB &#61; (13 | (64 << | PixelFormatAlpha | PixelFormatCanonical | PixelFormatExtended),
PixelFormat64bppPARGB &#61; (14 | (64 << | PixelFormatAlpha | PixelFormatPAlpha | PixelFormatExtended),
PixelFormatMax &#61; 15
}
// Pulled from imaging.h in the Windows Mobile 5.0 Pocket PC SDK
public enum BufferDisposalFlag : int
{
BufferDisposalFlagNone,
BufferDisposalFlagGlobalFree,
BufferDisposalFlagCoTaskMemFree,
BufferDisposalFlagUnmapView
}
// Pulled from imaging.h in the Windows Mobile 5.0 Pocket PC SDK
public enum InterpolationHint : int
{
InterpolationHintDefault,
InterpolationHintNearestNeighbor,
InterpolationHintBilinear,
InterpolationHintAveraging,
InterpolationHintBicubic
}
// Pulled from gdiplusimaging.h in the Windows Mobile 5.0 Pocket PC SDK
public struct BitmapData
{
public uint Width;
public uint Height;
public int Stride;
public PixelFormatID PixelFormat;
public IntPtr Scan0;
public IntPtr Reserved;
}
// Pulled from imaging.h in the Windows Mobile 5.0 Pocket PC SDK
public struct ImageInfo
{
public uint GuidPart1; // I am being lazy here, I don&#39;t care at this point about the RawDataFormat GUID
public uint GuidPart2; // I am being lazy here, I don&#39;t care at this point about the RawDataFormat GUID
public uint GuidPart3; // I am being lazy here, I don&#39;t care at this point about the RawDataFormat GUID
public uint GuidPart4; // I am being lazy here, I don&#39;t care at this point about the RawDataFormat GUID
public PixelFormatID pixelFormat;
public uint Width;
public uint Height;
public uint TileWidth;
public uint TileHeight;
public double Xdpi;
public double Ydpi;
public uint Flags;
}
// Pulled from imaging.h in the Windows Mobile 5.0 Pocket PC SDK
[ComImport, Guid("327ABDA7-072B-11D3-9D7B-0000F81EF32E"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComVisible(true)]
public interface IImagingFactory
{
uint CreateImageFromStream(); // This is a place holder, note the lack of arguments
uint CreateImageFromFile(string filename, out INativeImage image);
// We need the MarshalAs attribute here to keep COM interop from sending the buffer down as a Safe Array.
uint CreateImageFromBuffer([MarshalAs(UnmanagedType.LPArray)] byte[] buffer, uint size, BufferDisposalFlag disposalFlag, out INativeImage image);
uint CreateNewBitmap(uint width, uint height, PixelFormatID pixelFormat, out IBitmapImage bitmap);
uint CreateBitmapFromImage(INativeImage image, uint width, uint height, PixelFormatID pixelFormat, InterpolationHint hints, out IBitmapImage bitmap);
uint CreateBitmapFromBuffer(); // This is a place holder, note the lack of arguments
uint CreateImageDecoder(); // This is a place holder, note the lack of arguments
uint CreateImageEncoderToStream(); // This is a place holder, note the lack of arguments
uint CreateImageEncoderToFile(); // This is a place holder, note the lack of arguments
uint GetInstalledDecoders(); // This is a place holder, note the lack of arguments
uint GetInstalledEncoders(); // This is a place holder, note the lack of arguments
uint InstallImageCodec(); // This is a place holder, note the lack of arguments
uint UninstallImageCodec(); // This is a place holder, note the lack of arguments
}
// Pulled from imaging.h in the Windows Mobile 5.0 Pocket PC SDK
[ComImport, Guid("327ABDA9-072B-11D3-9D7B-0000F81EF32E"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComVisible(true)]
public interface INativeImage
{
uint GetPhysicalDimension(out Size size);
uint GetImageInfo(out ImageInfo info);
uint SetImageFlags(uint flags);
uint Draw(IntPtr hdc, ref Rectangle dstRect, IntPtr NULL); // "Correct" declaration: uint Draw(IntPtr hdc, ref Rectangle dstRect, ref Rectangle srcRect);
uint PushIntoSink(); // This is a place holder, note the lack of arguments
uint GetThumbnail(uint thumbWidth, uint thumbHeight, out INativeImage thumbImage);
}
// Pulled from imaging.h in the Windows Mobile 5.0 Pocket PC SDK
[ComImport, Guid("327ABDAA-072B-11D3-9D7B-0000F81EF32E"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComVisible(true)]
public interface IBitmapImage
{
uint GetSize(out Size size);
uint GetPixelFormatID(out PixelFormatID pixelFormat);
uint LockBits(ref Rectangle rect, uint flags, PixelFormatID pixelFormat, out BitmapData lockedBitmapData);
uint UnlockBits(ref BitmapData lockedBitmapData);
uint GetPalette(); // This is a place holder, note the lack of arguments
uint SetPalette(); // This is a place holder, note the lack of arguments
}
}


调用方法如下

using (Graphics gxBuffer &#61; Graphics.FromImage(backBuffer))
{
// Since we nop&#39;d OnPaintBackground, take care of it here
gxBuffer.Clear(this.BackColor);
// Ask the Image object from Imaging to draw itself.
IntPtr hdcDest &#61; gxBuffer.GetHdc();
Rectangle dstRect &#61; new Rectangle(100, 100, 148, 148);
imagingResource.Draw(hdcDest, ref dstRect, IntPtr.Zero);
gxBuffer.ReleaseHdc(hdcDest);
// Ask the Image object from Imaging to draw itself.
/*IntPtr*/ hdcDest &#61; gxBuffer.GetHdc(); // This is redundant, but keeps the necessary code together
/*Rectangle*/ dstRect &#61; new Rectangle(50, 70, 50&#43;132, 70&#43;132);
imagingImage.Draw(hdcDest, ref dstRect, IntPtr.Zero);
gxBuffer.ReleaseHdc(hdcDest);
}
// Put the final composed image on screen.
e.Graphics.DrawImage(backBuffer, 0, 0);


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

另外一种方法还是采用Manged code, 对于预先知道透明色值的图像&#xff0c;比如地图API中的路径&#xff0c;背景色总为0xFFE0E0E0
可以使用下面方法

 

 

ImageAttributes _imageAttributes &#61; new ImageAttributes();
Color color &#61; Color.FromArgb(0xE0E0E0);
_imageAttributes.SetColorKey(color, color);
Rectangle dstRect &#61;new Rectangle(x, y, netImage.GetWidth(), netImage.GetHeight());
gxBuffer.DrawImage(netImage._bitmap, dstRect, 0, 0,netImage.GetWidth(),
netImage.GetHeight(),
GraphicsUnit.Pixel, _imageAttributes);


最终结果如下图

 

 


推荐阅读
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文介绍了C#中生成随机数的三种方法,并分析了其中存在的问题。首先介绍了使用Random类生成随机数的默认方法,但在高并发情况下可能会出现重复的情况。接着通过循环生成了一系列随机数,进一步突显了这个问题。文章指出,随机数生成在任何编程语言中都是必备的功能,但Random类生成的随机数并不可靠。最后,提出了需要寻找其他可靠的随机数生成方法的建议。 ... [详细]
  • Metasploit攻击渗透实践
    本文介绍了Metasploit攻击渗透实践的内容和要求,包括主动攻击、针对浏览器和客户端的攻击,以及成功应用辅助模块的实践过程。其中涉及使用Hydra在不知道密码的情况下攻击metsploit2靶机获取密码,以及攻击浏览器中的tomcat服务的具体步骤。同时还讲解了爆破密码的方法和设置攻击目标主机的相关参数。 ... [详细]
  • Python瓦片图下载、合并、绘图、标记的代码示例
    本文提供了Python瓦片图下载、合并、绘图、标记的代码示例,包括下载代码、多线程下载、图像处理等功能。通过参考geoserver,使用PIL、cv2、numpy、gdal、osr等库实现了瓦片图的下载、合并、绘图和标记功能。代码示例详细介绍了各个功能的实现方法,供读者参考使用。 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • 本文详细介绍了如何使用MySQL来显示SQL语句的执行时间,并通过MySQL Query Profiler获取CPU和内存使用量以及系统锁和表锁的时间。同时介绍了效能分析的三种方法:瓶颈分析、工作负载分析和基于比率的分析。 ... [详细]
  • 【shell】网络处理:判断IP是否在网段、两个ip是否同网段、IP地址范围、网段包含关系
    本文介绍了使用shell脚本判断IP是否在同一网段、判断IP地址是否在某个范围内、计算IP地址范围、判断网段之间的包含关系的方法和原理。通过对IP和掩码进行与计算,可以判断两个IP是否在同一网段。同时,还提供了一段用于验证IP地址的正则表达式和判断特殊IP地址的方法。 ... [详细]
  • 解决github访问慢的问题的方法集锦
    本文总结了国内用户在访问github网站时可能遇到的加载慢的问题,并提供了解决方法,其中包括修改hosts文件来加速访问。 ... [详细]
  • 本文讨论了在VMWARE5.1的虚拟服务器Windows Server 2008R2上安装oracle 10g客户端时出现的问题,并提供了解决方法。错误日志显示了异常访问违例,通过分析日志中的问题帧,找到了解决问题的线索。文章详细介绍了解决方法,帮助读者顺利安装oracle 10g客户端。 ... [详细]
  • React基础篇一 - JSX语法扩展与使用
    本文介绍了React基础篇一中的JSX语法扩展与使用。JSX是一种JavaScript的语法扩展,用于描述React中的用户界面。文章详细介绍了在JSX中使用表达式的方法,并给出了一个示例代码。最后,提到了JSX在编译后会被转化为普通的JavaScript对象。 ... [详细]
  • 本文介绍了使用Python编写购物程序的实现步骤和代码示例。程序启动后,用户需要输入工资,并打印商品列表。用户可以根据商品编号选择购买商品,程序会检测余额是否充足,如果充足则直接扣款,否则提醒用户。用户可以随时退出程序,在退出时打印已购买商品的数量和余额。附带了完整的代码示例。 ... [详细]
  • Android自定义控件绘图篇之Paint函数大汇总
    本文介绍了Android自定义控件绘图篇中的Paint函数大汇总,包括重置画笔、设置颜色、设置透明度、设置样式、设置宽度、设置抗锯齿等功能。通过学习这些函数,可以更好地掌握Paint的用法。 ... [详细]
  • Java 11相对于Java 8,OptaPlanner性能提升有多大?
    本文通过基准测试比较了Java 11和Java 8对OptaPlanner的性能提升。测试结果表明,在相同的硬件环境下,Java 11相对于Java 8在垃圾回收方面表现更好,从而提升了OptaPlanner的性能。 ... [详细]
author-avatar
mobiledu2502880603
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有