WPF GraphicsPath 를 PathGeometry 로 변환하는 코드입니다.
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 };
}
* 예시 코드는 진행했던 프로젝트에서 사용했던 것입니다.
WPF Font 적용하기 (0) | 2024.10.01 |
---|---|
WPF Freezable (0) | 2024.09.27 |
WPF Image Zoom 및 Panning 처리하기 (0) | 2024.09.23 |
WPF Image to BitmapImage (0) | 2024.09.11 |
WPF BooleanToVisibilityConverter (0) | 2024.09.11 |