先上图:

今天我们要实现的是DrawTool画笔集合中的一种纹理笔,很多人可能对纹理笔概念还比较生疏,其实如果你接触过类似一些教育行业交互式白板的话,对纹理笔并不会感到陌生,纹理笔我们可以简单的理解为是一种通过图片来呈现笔迹的方式.在继续下面的阅读前,你有必要先了解下以下知识:

通过上面的文章,我们知道DynamicRenderer为我们提供是呈现,而Stroke是最后我们生成笔迹,如果要实现纹理笔,我们需要重载DynamicRenderer的OnDraw和Stroke的DrawCore方法.

1. 自定义Stroke

public  class TextureStroke : Stroke
{
private System.Windows.Media.ImageSource imageSource_;
//纹理图片
private string textureFile_;
public TextureStroke(StylusPointCollection stylusPoints, DrawingAttributes da , string file )
: base(stylusPoints, da)
{
this.textureFile_ = file;
this.imageSource_ = new System.Windows.Media.Imaging.BitmapImage(new Uri(this.textureFile_));
} /// <summary>
/// 实现绘制
/// </summary>
/// <param name="drawingContext"></param>
/// <param name="drawingAttributes"></param>
protected override void DrawCore(DrawingContext drawingContext, DrawingAttributes drawingAttributes)
{
double width = drawingAttributes.Width * 2.0;
System.Windows.Media.StreamGeometry streamGeometry = new System.Windows.Media.StreamGeometry();
using (System.Windows.Media.StreamGeometryContext streamGeometryContext = streamGeometry.Open())
{
streamGeometryContext.BeginFigure((Point)base.StylusPoints[], false, false);
foreach (System.Windows.Input.StylusPoint current in base.StylusPoints)
{
streamGeometryContext.LineTo((Point)current, true, true);
}
streamGeometryContext.Close();
} DrawTexture(streamGeometry, this.imageSource_, drawingContext, width, this.textureFile_);
} public static void DrawTexture(Geometry geometry, ImageSource imageSource, DrawingContext drawingContext, double width, string imagePath)
{
Rect rc = geometry.Bounds;
DrawingBrush drawingBrush = new DrawingBrush(new ImageDrawing
{
Rect = rc,
ImageSource = imageSource
});
drawingBrush.TileMode = TileMode.Tile;
Uri uriSource = new Uri(imagePath);
BitmapImage bitmapImage = new BitmapImage(uriSource);
drawingBrush.Viewport = new Rect(0.0, 0.0, (double)bitmapImage.PixelWidth, (double)bitmapImage.PixelHeight);
drawingBrush.ViewportUnits = BrushMappingMode.Absolute;
PathGeometry widenedPathGeometry = geometry.GetWidenedPathGeometry(new Pen(null, width)
{
StartLineCap = PenLineCap.Round,
EndLineCap = PenLineCap.Round
});
Pen pen = new Pen(drawingBrush, 80.0);
drawingContext.PushClip(widenedPathGeometry);
drawingContext.DrawRectangle(drawingBrush, pen, rc);
drawingContext.Pop();
}
}

2.实例应用

这里我们使用到上篇文章中介绍的多点触摸画板 ,修改其中_startStroke方法中stroke对象的生成,相应代码如下:

bool isTexturePen = true ; // 外部定义的私有变量

private void _startStroke(object device, Point inputPosition){
// ... { some code } //==这里根据变量isTexturePen来判断使用默认的stroke还是自定义的stroke
Stroke stroke = isTexturePen==true ? new TextureStroke( stylusPointCollection , this._inkCanvas.DefaultDrawingAttributes,"c:\\1.png") : new Stroke(stylusPointCollection);
//end // .... { some code }
}

3. 结束

DrawTool系列文章中涉及的代码都是通过修改后的,仅仅适用于demo场景,如果需要商业请自行修改,包括性能上的调整.

最新文章

  1. Swift开发第二篇——extension及fatalError
  2. Ganglia安装扩容
  3. js高级程序设计(六)面向对象
  4. FineUI中Newtonsoft.Json版本报错解决办法
  5. JiaThis WordPress分享插件安装全攻略
  6. javascript 构造函数中的属性与原型上属性优先级的比较
  7. linux概念之用户,组及权限
  8. 电脑右键新建文本文档(txt)消失的解决办法
  9. STL--string(转载)
  10. http协议与内容压缩
  11. jmap命令结合mat插件分析内存泄露--OQL
  12. js修改window对象中的url历史记录
  13. Helper Method
  14. 《Windows驱动开发技术详解》之HelloDDK
  15. VMware Workstation 12 Pro 之安装林耐斯-Elementaryos-系统
  16. StringUtils工具类常用方法汇总2(截取、去除空白、包含、查询索引)
  17. NJU 1010 Air
  18. 利用scrapy模拟登录知乎
  19. 小程序实现GBK编码数据转为Unicode/UTF8
  20. JS实现数组去重方法大总结

热门文章

  1. 洛谷 1541 乌龟棋——dp
  2. bzoj 2194 快速傅立叶之二 —— FFT
  3. zlog 纯C日志函数库的简单使用方法
  4. 解压缩zip,tar,tar.gz,tar.bz2文件
  5. 二、myeclipse中配置mybatis中xml的自动提示
  6. Tautonym Puzzle
  7. 3.2-3.3 Hive中常见的数据压缩
  8. MessageFomat学习
  9. C#简单的国际化
  10. lightoj 1035【暴力】