热门标签 | 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);


最终结果如下图

 

 


推荐阅读
  • Imtryingtofigureoutawaytogeneratetorrentfilesfromabucket,usingtheAWSSDKforGo.我正 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • 本文介绍了一个题目的解法,通过二分答案来解决问题,但困难在于如何进行检查。文章提供了一种逃逸方式,通过移动最慢的宿管来锁门时跑到更居中的位置,从而使所有合格的寝室都居中。文章还提到可以分开判断两边的情况,并使用前缀和的方式来求出在任意时刻能够到达宿管即将锁门的寝室的人数。最后,文章提到可以改成O(n)的直接枚举来解决问题。 ... [详细]
  • 3.223.28周学习总结中的贪心作业收获及困惑
    本文是对3.223.28周学习总结中的贪心作业进行总结,作者在解题过程中参考了他人的代码,但前提是要先理解题目并有解题思路。作者分享了自己在贪心作业中的收获,同时提到了一道让他困惑的题目,即input details部分引发的疑惑。 ... [详细]
  • Linux如何安装Mongodb的详细步骤和注意事项
    本文介绍了Linux如何安装Mongodb的详细步骤和注意事项,同时介绍了Mongodb的特点和优势。Mongodb是一个开源的数据库,适用于各种规模的企业和各类应用程序。它具有灵活的数据模式和高性能的数据读写操作,能够提高企业的敏捷性和可扩展性。文章还提供了Mongodb的下载安装包地址。 ... [详细]
  • 也就是|小窗_卷积的特征提取与参数计算
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了卷积的特征提取与参数计算相关的知识,希望对你有一定的参考价值。Dense和Conv2D根本区别在于,Den ... [详细]
  • FeatureRequestIsyourfeaturerequestrelatedtoaproblem?Please ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • Ihavethefollowingonhtml我在html上有以下内容<html><head><scriptsrc..3003_Tes ... [详细]
  • 本文讨论了编写可保护的代码的重要性,包括提高代码的可读性、可调试性和直观性。同时介绍了优化代码的方法,如代码格式化、解释函数和提炼函数等。还提到了一些常见的坏代码味道,如不规范的命名、重复代码、过长的函数和参数列表等。最后,介绍了如何处理数据泥团和进行函数重构,以提高代码质量和可维护性。 ... [详细]
  • Android系统源码分析Zygote和SystemServer启动过程详解
    本文详细解析了Android系统源码中Zygote和SystemServer的启动过程。首先介绍了系统framework层启动的内容,帮助理解四大组件的启动和管理过程。接着介绍了AMS、PMS等系统服务的作用和调用方式。然后详细分析了Zygote的启动过程,解释了Zygote在Android启动过程中的决定作用。最后通过时序图展示了整个过程。 ... [详细]
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社区 版权所有