Silverlight 引路蜂二维图形库示例:概述

Silverlight 引路蜂二维图形库示例:概述,第1张

概述Windows Phone 7 并不是Windows Mobile 6.x 的升级版,也就是说 Windows Phone 7 SDK 没有提供对 Windows Mobile .Net Compact Version的向下兼容。“它所带来的后果之一就是,之前的Windows Moible应用程序无法在Windows Phone 7系列上运行。” Windows Phone 7 SDK 是基于Si

windows Phone 7 并不是windows Mobile 6.x 的升级版,也就是说 windows Phone 7 SDK 没有提供对 windows Mobile .Net Compact Version的向下兼容。“它所带来的后果之一就是,之前的windows Moible应用程序无法在windows Phone 7系列上运行。” windows Phone 7 SDK 是基于Silverlight。因此一些原先定义在.Net Compact Framework中的包和类在 Window Phone 7 SDK 中没有对应的类。比如System.Drawing,System.Drawing.Image 。在windows Phone 7 SDK 提供类似功能的是System.Windows.Shapes和System.Windows.Media.Imaging 。当如果想动态生成二维图形或是图像,Silverlight和 GDI+ 相比不是特别方便。

引路蜂开发包中提供了一个平台无关的高效二维图形库,提供了诸如lineCap,lineJoin,Brush,TextBrush,Path 等类。它的画板为一个二维数组(内部采用一维数组),每个数组元素为一个32位整数,代表ARGB颜色值。SilverlightSystem.Windows.Media.Imaging 中定义了WriteableBitmap Class ,WriteableBitmap的Pixels属性也是一个整数数组,其定义方法和引路蜂开发包中定义的一致。因此通过WriteableBitmap作为桥梁,就可以把引路蜂开发包Graphics2D绘制的图形图像显示到Silverlight的XAML 的Image 元素中。

下图显示了Silverlight 引路蜂二维图形开发包中定义的包和类。

最后以一个简单的例子说明一下图形包的用法。也可以参见Windows Mobile引路蜂地图开发示例:二维图形库。

创建一个Silverlight项目,修改MainPage.xml ,如下所示。

<UserControl x:Class=”SilverlightGraphics2DDemo.MainPage”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
xmlns:d=”http://schemas.microsoft.com/expression/blend/2008”
xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006”
mc:Ignorable=”d”
d:DesignHeight=”640″ d:DesignWIDth=”480″>

<GrID x:name=”LayoutRoot” WIDth=”480″ Height=”640″>
<StackPanel>
<TextBlock HorizontalAlignment=”Center”
FontSize=”14″ Text=”Silverlight Graphics 2D Demo” />
<border WIDth=”480″ Height=”640″ Background=”Black”>
<Image x:name=”image” WIDth=”480″ Height=”640″/>
</border>
</StackPanel>
</GrID>
< /UserControl>

XAML中定义了一个Image对象用于显示图像显示的结果。

修改MainPage.xaml.cs

using System;using System.windows.Controls;using System.windows.Media.Imaging;using Mapdigit.Drawing;using Mapdigit.Drawing.Geometry;using color = Mapdigit.Drawing.color;namespace SilverlightGraphics2DDemo{    public partial class MainPage : UserControl    {        private Graphics2D graphics2D;        private int screenWIDth = 480;        private int screenHeight = 640;         WriteableBitmap bmp;        public MainPage()        {            InitializeComponent();            graphics2D = new Graphics2D(screenWIDth,screenHeight);            Ellipse circle,oval,leaf,stem;            Area circ,ov,leaf1,leaf2,st1,st2;            circle = new Ellipse();            oval = new Ellipse();            leaf = new Ellipse();            stem = new Ellipse();            circ = new Area(circle);            ov = new Area(oval);            leaf1 = new Area(leaf);            leaf2 = new Area(leaf);            st1 = new Area(stem);            st2 = new Area(stem);            graphics2D.reset();            graphics2D.Clear(color.White);            int w = screenWIDth;            int h = screenHeight;            int ew = w / 2;            int eh = h / 2;            SolIDBrush brush = new SolIDBrush(color.Green);            graphics2D.DefaultBrush = brush;            // Creates the first leaf by filling the intersection of two Area            //objects created from an ellipse.            leaf.SetFrame(ew - 16,eh - 29,15,15);            leaf1 = new Area(leaf);            leaf.SetFrame(ew - 14,eh - 47,30,30);            leaf2 = new Area(leaf);            leaf1.Intersect(leaf2);            graphics2D.Fill(null,leaf1);             // Creates the second leaf.            leaf.SetFrame(ew + 1,15);            leaf1 = new Area(leaf);            leaf2.Intersect(leaf1);            graphics2D.Fill(null,leaf2);             brush = new SolIDBrush(color.Black);            graphics2D.DefaultBrush = brush;             // Creates the stem by filling the Area resulting from the            //subtraction of two Area objects created from an ellipse.            stem.SetFrame(ew,eh - 42,40,40);            st1 = new Area(stem);            stem.SetFrame(ew + 3,50,50);            st2 = new Area(stem);            st1.Subtract(st2);            graphics2D.Fill(null,st1);             brush = new SolIDBrush(color.Yellow);            graphics2D.DefaultBrush = brush;             // Creates the pear itself by filling the Area resulting from the            //union of two Area objects created by two different ellipses.            circle.SetFrame(ew - 25,eh,50);            oval.SetFrame(ew - 19,eh - 20,70);            circ = new Area(circle);            ov = new Area(oval);            circ.Add(ov);            graphics2D.Fill(null,circ);             bmp = new WriteableBitmap(screenWIDth,screenHeight);            image.source = bmp;            RefreshBitmap();        }        private voID RefreshBitmap()        {            Buffer.Blockcopy(graphics2D.Argb,bmp.Pixels,bmp.Pixels.Length * 4);        }     }}

Buffer.Blockcopy(graphics2D.Argb,bmp.Pixels.Length * 4);就是将Graphics2D内部int32数组拷贝到WriteableBitmap的Pixels中。上面代码通过多个几何图形拼成一个梨子如下图所示:

Graphics2D 示例

总结

以上是内存溢出为你收集整理的Silverlight 引路蜂二维图形库示例:概述全部内容,希望文章能够帮你解决Silverlight 引路蜂二维图形库示例:概述所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1067653.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存