事实上很早以前就有人开发了一个叫 XrossOne Mobile GDI+ 的开源二维图形引擎,它完全是用 C# 托管代码编写的。XrossOne GDI+ 可以帮助 .NET Compact Framework 开发人员创建高质量的矢量图形输出。GDI+ 中的所有高级功能(反锯齿绘图、线帽/联接装饰、二维变换、渐变填充等等)几乎都可以通过它实现。不过由于 XrossOne GDI+ 是纯托管代码实现的,在绘图性能上跟 .NET Framework 的 GDI+ 还有一定的差距。在《用于 Microsoft .NET Compact Framework 的 XrossOne Mobile》一文中可以了解到 XrossOne GDI+ 的详细介绍及源代码下载。
Windows Mobile 还提供了 Game API 和 Direct3D Mobile,但这两套 API 是针对游戏程序的,并不一定适合普通的应用程序。
事实上从 Windows Mobile 5.0 开始就支持 GDI+ 了,开发人员可以利用 C 语言或者 P/Invoke 来使用这些 API。OpenNETCF 顾问 Alex Feinman 已经将 Windows Mobile 的 GDI+ API 用 C# 语言封装好了,并且提供了一些很漂亮的示例程序。
Brushes Brush Demo source code: PathGradient brush // Create rectangular path GraphicsPath path = new GraphicsPath(FillMode.FillModeAlternate); path.AddRectangle(new GpRectF( 0, 0, ClientRectangle.Width, ClientRectangle.Height / 5)); // Create rectangular gradient brush // with red in center and black in the corners brPathGrad = new PathGradientBrush(path); brPathGrad.SetCenterColor(Color.Red); int count = 2; brPathGrad.SetSurroundColors(new Color[] { Color.Black, Color.Black }, ref count); Solid Brush brSolid = new SolidBrushPlus(Color.CornflowerBlue); Hatch Brush brHatch = new HatchBrush(HatchStyle.HatchStyle25Percent, Color.Black, Color.White); Linear Gradient brLinGrad = new LinearGradientBrush(new GpPointF(0, 0), new GpPointF(50, 50), Color.Black, Color.White); Texture brush StreamOnFile sf = new StreamOnFile(bitmapPath); ImagePlus img = new ImagePlus(sf, false); brTexture = new TextureBrushPlus(img, WrapMode.WrapModeTile); Pens Pen Demo source code: Solid with caps. Standard caps are used – round and arrow brSolid = new SolidBrushPlus(Color.CornflowerBlue); penSolid = new PenPlus(Color.Red, 10); penSolid.SetEndCap(LineCap.LineCapRound); penSolid.SetStartCap(LineCap.LineCapArrowAnchor); Solid with caps and antialiasing. This one is the same as before except it is drawn with antialiasing g.SetSmoothingMode(SmoothingMode.SmoothingModeAntiAlias); penSolid.SetColor(Color.Blue); g.DrawLine(penSolid, 5, rcf.Top + 10, rc.Width - 10, rcf.Top + 10); Hatched (25%) brHatch = new HatchBrush(HatchStyle.HatchStyle25Percent, Color.Black, Color.White); penHatch = new PenPlus(brHatch, 10); Solid with transparency penSolidTrans = new PenPlus(Color.FromArgb(-0x5f7f7f7f), 10); Custom cap. The custom cap has been created out of a path object consisting of a single ellipse penSolidCustomCap = new PenPlus(Color.Black, 20); GraphicsPath path = new GraphicsPath(FillMode.FillModeAlternate); path.AddEllipse(-0.5f, -1.5f, 1, 3); CustomLineCap cap = new CustomLineCap(null,path, LineCap.LineCapFlat, 0); penSolidCustomCap.SetCustomEndCap(cap); Dash penDash = new PenPlus(Color.Black, 5); penDash.SetDashStyle(DashStyle.DashStyleDot); Gradient brush-based brGrad = new LinearGradientBrush( new GpPointF(0, 0), new GpPointF(100, 100), Color.Black, Color.White); penGradient = new PenPlus(brGrad, 30); Demo application
效果还不错吧!既然是用 Win32 API 实现绘图的,性能肯定不差。关于这套 GDI+ API 的详细信息,可以仔细看 Alex Feinman 的《Using GDI+ on Windows Mobile》,还提供源代码和文章的PDF格式下载哦!