using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows; using System.Windows.Media;
/// <summary> /// Convert GraphicsPath To PathGeometry /// </summary> /// <param name="path">System.Drawing.Drawing2D.GraphicsPath</param> /// <returns>System.Windows.Media.PathGeometry</returns> public static PathGeometry ConvertGraphicsPathToPathGeometry(GraphicsPath path) { List<PathFigure> pathFigureList = new List<PathFigure>();
if (path.PointCount != 0) { PointF[] pathPoints = path.PathPoints; byte[] pathTypes = path.PathTypes; PathFigure pathFigure = new PathFigure(); pathFigureList.Add(pathFigure); int index = 0; while (index < pathPoints.Length) { switch ((int)pathTypes[index] & 7) { case 0: System.Windows.Point windowsPoint1 = new System.Windows.Point((double)pathPoints[index].X, (double)pathPoints[index].Y); ; pathFigure.StartPoint = windowsPoint1; ++index; break; case 1: System.Windows.Point windowsPoint2 = new System.Windows.Point((double)pathPoints[index].X, (double)pathPoints[index].Y); pathFigure.Segments.Add((PathSegment)new LineSegment(windowsPoint2, true)); ++index; break; case 3: System.Windows.Point windowsPoint3 = new System.Windows.Point((double)pathPoints[index].X, (double)pathPoints[index].Y); System.Windows.Point windowsPoint4 = new System.Windows.Point((double)pathPoints[index + 1].X, (double)pathPoints[index + 1].Y); System.Windows.Point windowsPoint5 = new System.Windows.Point((double)pathPoints[index + 2].X, (double)pathPoints[index + 2].Y); pathFigure.Segments.Add((PathSegment)new BezierSegment(windowsPoint3, windowsPoint4, windowsPoint5, true)); index += 3; break; }
if (((int)pathTypes[index - 1] & 128) != 0) { pathFigure.IsClosed = true; pathFigure = new PathFigure(); pathFigureList.Add(pathFigure); } } }
return new PathGeometry((IEnumerable<PathFigure>)pathFigureList) { FillRule = path.FillMode == FillMode.Alternate ? FillRule.EvenOdd : FillRule.Nonzero }; } |
'C# > WPF' 카테고리의 다른 글
[WPF] Image 에 Angle 있는 사각형 그리기 (0) | 2021.05.22 |
---|---|
[WPF] wpf 분석 필요한 소스 (Vintasoft.Imaging.Wpf.WpfObjectConverter) (0) | 2021.05.13 |
[WPF] Convert hex color value to SolidColorBrush (0) | 2021.04.05 |
[WPF] TextBlock 개행(줄바꿈) 처리 (0) | 2021.01.28 |
[WPF] xaml 로 숫자 소숫점 세자리까지 표시하기 (0) | 2021.01.18 |