반응형

 

 

 

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 };

}

 

반응형

+ Recent posts