实例介绍
【实例简介】
【实例截图】

【核心代码】
public class ChinesebrushCanvas : InkCanvas
{
public ChinesebrushCanvas()
{
//当然要换上我们特地搞出来的ChinesebrushRenderer
this.DynamicRenderer = new ChinesebrushRenderer();
}
protected override void OnStrokeCollected(InkCanvasStrokeCollectedEventArgs e)
{
//感兴趣的童鞋,注释这一句看看?
this.Strokes.Remove(e.Stroke);
this.Strokes.Add(new ChinesebrushStroke(e.Stroke.StylusPoints, this.DefaultDrawingAttributes.Color));
}
}
public class ChinesebrushRenderer : DynamicRenderer
{
private ImageSource imageSource;
private readonly double width = 15;
protected override void OnDrawingAttributesReplaced()
{
if (DesignerProperties.GetIsInDesignMode(this.Element))
return;
base.OnDrawingAttributesReplaced();
var dv = new DrawingVisual();
var size = 90;
using (var conext = dv.RenderOpen())
{
//[关键]OpacityMask了解下?也许有童鞋想到的办法是,各种颜色的图片来一张?
//conext.PushOpacityMask(new ImageBrush(new BitmapImage(new Uri("pack://application:,,,/pen.png"))));
conext.PushOpacityMask(new ImageBrush(new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory "Images\\pen.png", UriKind.Absolute))));
//用颜色生成画笔画一个矩形
conext.DrawRectangle(new SolidColorBrush(this.DrawingAttributes.Color), null, new Rect(0, 0, size, size));
conext.Close();
}
var rtb = new RenderTargetBitmap(size, size,96d, 96d, PixelFormats.Pbgra32);
rtb.Render(dv);
imageSource = BitmapFrame.Create(rtb);
//[重要]此乃解决卡顿问题的关键!
imageSource.Freeze();
}
protected override void OnDraw(DrawingContext drawingContext, StylusPointCollection stylusPoints, Geometry geometry, Brush fillBrush)
{
var p1 = new Point(double.NegativeInfinity, double.NegativeInfinity);
var p2 = new Point(0, 0);
var w1 = this.width 5;
for (int i = 0; i < stylusPoints.Count; i )
{
p2 = (Point)stylusPoints[i];
//两点相减得到一个向量[高中数学知识了解下?]
var vector = p1 - p2;
//得到 x、y的变化值
var dx = (p2.X - p1.X) / vector.Length;
var dy = (p2.Y - p1.Y) / vector.Length;
var w2 = this.width;
if (w1 - vector.Length > this.width)
w2 = w1 - vector.Length;
//为啥又来一个for?图像重叠,实现笔画的连续性,感兴趣的童鞋可以把for取消掉看看效果
for (int j = 0; j < vector.Length; j )
{
var x = p2.X;
var y = p2.Y;
if (!double.IsInfinity(p1.X) && !double.IsInfinity(p1.Y))
{
x = p1.X dx;
y = p1.Y dy;
}
//画图,没啥可说的
drawingContext.DrawImage(imageSource, new Rect(x - w2 / 2.0, y - w2 / 2.0, w2, w2));
//再把新的坐标赋值给p1,以序后来
p1 = new Point(x, y);
if (double.IsInfinity(vector.Length))
break;
}
}
}
}
public class ChinesebrushStroke : Stroke
{
private ImageSource imageSource;
private readonly double width = 15;
public ChinesebrushStroke(StylusPointCollection stylusPointCollection, Color color) : base(stylusPointCollection)
{
if (DesignerProperties.GetIsInDesignMode(App.Current.MainWindow))
return;
var dv = new DrawingVisual();
var size = 90;
using (var conext = dv.RenderOpen())
{
conext.PushOpacityMask(new ImageBrush(new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory "Images\\pen.png", UriKind.Absolute))));
conext.DrawRectangle(new SolidColorBrush(color), null, new Rect(0, 0, size, size));
conext.Close();
}
var rtb = new RenderTargetBitmap(size, size, 96d, 96d, PixelFormats.Pbgra32);
rtb.Render(dv);
imageSource = BitmapFrame.Create(rtb);
//Freezable 类提供特殊功能,以便在使用修改或复制开销很大的对象时帮助提高应用程序性能
//WPF中的Frozen(冻结)与线程及其他相关问题
imageSource.Freeze();
}
//卡顿就是该函数造成,每写完一笔就会调用,当笔画过长,后果可想而知~
protected override void DrawCore(DrawingContext drawingContext, DrawingAttributes drawingAttributes)
{
if (this.StylusPoints?.Count < 1)
return;
var p1 = new Point(double.NegativeInfinity, double.NegativeInfinity);
var w1 = this.width 5;
for (int i = 0; i < StylusPoints.Count; i )
{
var p2 = (Point)this.StylusPoints[i];
var vector = p1 - p2;
var dx = (p2.X - p1.X) / vector.Length;
var dy = (p2.Y - p1.Y) / vector.Length;
var w2 = this.width;
if (w1 - vector.Length > this.width)
w2 = w1 - vector.Length;
for (int j = 0; j < vector.Length; j )
{
var x = p2.X;
var y = p2.Y;
if (!double.IsInfinity(p1.X) && !double.IsInfinity(p1.Y))
{
x = p1.X dx;
y = p1.Y dy;
}
drawingContext.DrawImage(imageSource, new Rect(x - w2 / 2.0, y - w2 / 2.0, w2, w2));
p1 = new Point(x, y);
if (double.IsInfinity(vector.Length))
break;
}
}
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论