今天在蓝色上看到一篇介绍WritableBitmapEx的贴子(是开源项目),项目地址:http://writeablebitmapex.codeplex.com/
对SL的WritableBitmap做了一些扩展,可以方便的绘制一些基本几何形状,示例代码如下:
代码
using
System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace WriteableBitmapExDemo
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this .Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded( object sender, RoutedEventArgs e)
{
WriteableBitmap _wb = new WriteableBitmap( 512 , 512 ); // 新建一个512px * 512px的可写位图
this .ImgDemo.Source = _wb;
_wb.Clear(Colors.White); // 填充背景色为白色
// 从(50,0)到(0,50)划一根线,颜色为绿色
_wb.DrawLine( 50 , 0 , 0 , 50 , Colors.Magenta);
// 从(100, 100)到(50,187)再到(150,187)划一个黑色三角形
_wb.DrawTriangle( 100 , 100 , 50 , 187 , 150 , 187 , Colors.Black);
// 从(50, 50) 到(100,100)划一个红色的矩形
_wb.DrawRectangle( 50 , 50 , 100 , 100 , Colors.Red);
// 从(200,200)为中心,划一个横轴为100,竖轴为50的椭圆
_wb.DrawEllipseCentered( 200 , 200 , 100 , 50 , Colors.Blue);
// 划一个多边形 P1(10, 5), P2(20, 40), P3(30, 30) and P4(7, 8)
int [] p = new int [] { 10 , 5 , 20 , 40 , 30 , 30 , 7 , 8 , 10 , 5 };
_wb.DrawPolyline(p, Colors.Green);
// Present the WriteableBitmap!
_wb.Invalidate(); // 实际测试下来,好象不加这一行也可以运行
}
}
}
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace WriteableBitmapExDemo
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this .Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded( object sender, RoutedEventArgs e)
{
WriteableBitmap _wb = new WriteableBitmap( 512 , 512 ); // 新建一个512px * 512px的可写位图
this .ImgDemo.Source = _wb;
_wb.Clear(Colors.White); // 填充背景色为白色
// 从(50,0)到(0,50)划一根线,颜色为绿色
_wb.DrawLine( 50 , 0 , 0 , 50 , Colors.Magenta);
// 从(100, 100)到(50,187)再到(150,187)划一个黑色三角形
_wb.DrawTriangle( 100 , 100 , 50 , 187 , 150 , 187 , Colors.Black);
// 从(50, 50) 到(100,100)划一个红色的矩形
_wb.DrawRectangle( 50 , 50 , 100 , 100 , Colors.Red);
// 从(200,200)为中心,划一个横轴为100,竖轴为50的椭圆
_wb.DrawEllipseCentered( 200 , 200 , 100 , 50 , Colors.Blue);
// 划一个多边形 P1(10, 5), P2(20, 40), P3(30, 30) and P4(7, 8)
int [] p = new int [] { 10 , 5 , 20 , 40 , 30 , 30 , 7 , 8 , 10 , 5 };
_wb.DrawPolyline(p, Colors.Green);
// Present the WriteableBitmap!
_wb.Invalidate(); // 实际测试下来,好象不加这一行也可以运行
}
}
}
官方还给出二个演示的地址:
http://dl.dropbox.com/u/2681028/CodeplexData/WriteableBitmapEx/ShapeSample/TestPage.html
http://dl.dropbox.com/u/2681028/CodeplexData/WriteableBitmapEx/BlitSample/TestPage.html
理论上讲,只要精通图形算法再加上一点艺术细胞,直接用cs代码画出一个MM来是可行的 :)