先上效果图吧
同样老规矩,先上源码地址:https://gitee.com/akwkevin/aistudio.-wpf.-diagram
本次实现的内容有
- [1] 画笔实现
- [2] 封闭画笔实现
- [3] 钢笔实现
- [4] 文字画笔
- [5] 直线,矩形,椭圆
- [6] Path形状
- [7] 取色器
- [8] 三种画笔可选
画笔示例入口
示例截图
核心代码:
1.Ellipse的实现:Geometry = new EllipseGeometry(new Rect(Points[0], Points[1]));
2.Rectangle的实现: Geometry = new RectangleGeometry(new Rect(Points[0], Points[1]));
3.Line的实现: Geometry = new LineGeometry(Points[0], Points[1]);
4.文本的实现:
var formattedText = new FormattedText(Text,
System.Globalization.CultureInfo.InvariantCulture,
FlowDirection.LeftToRight,
typeface,
FontViewModel.FontSize,
new SolidColorBrush(FontViewModel.FontColor));
Geometry = formattedText.BuildGeometry(new Point()).GetFlattenedPathGeometry();
5.多点连接线
var geometry = new PathGeometry();
var figure = new PathFigure { StartPoint = Points[0] };
geometry.Figures.Add(figure);
for (int i = 1; i < Points.Count; i++)
{
LineSegment arc = new LineSegment(Points[i], true);
geometry.Figures[0].Segments.Add(arc);
}
Geometry = geometry;
6.Path形状
var path = GetPath();
PathGeometry pathGeometry = PathGeometry.CreateFromGeometry(Geometry.Parse(path));
var transformGroup = new TransformGroup();
double radiox = Math.Abs(Points[1].X - Points[0].X) / pathGeometry.Bounds.Width;
double radioy = Math.Abs(Points[1].Y - Points[0].Y) / pathGeometry.Bounds.Height;
transformGroup.Children.Add(new TranslateTransform((Points[0].X) / radiox - pathGeometry.Bounds.Left, (Points[0].Y) / radioy - pathGeometry.Bounds.Top));
transformGroup.Children.Add(new ScaleTransform(radiox, radioy));
pathGeometry.Transform = transformGroup;
Geometry = pathGeometry;
7.橡皮的实现
erase.Transform = new TranslateTransform(0 - Left, 0 - Top);
Geometry = Geometry.Combine(Geometry, erase, GeometryCombineMode.Exclude, null);
8.取色器的实现
var point = CursorPointManager.GetCursorPosition();
Root.CurrentColor = ColorPickerManager.GetColor(point.X, point.Y);
讲一个关键点:形状要可擦除,必须要转换一下
Geometry = Geometry.GetWidenedPathGeometry(aPen);
今天实在有点懒得搞,就这样吧。
标签:画笔,Geometry,pathGeometry,画板,Diagram,Points,var,new,Wpf From: https://www.cnblogs.com/akwkevin/p/17417546.html