PathResourceConverter

PathResourceConverter class

转换PathResourceGraphicsPath反之亦然。

public static class PathResourceConverter

方法

姓名描述
static FromGraphicsPath(GraphicsPath, Size)转换GraphicsPath实例到路径资源。
static ToGraphicsPath(PathResource[], Size)将路径资源转换为GraphicsPath实例.

例子

从 TIFF 图像中的路径资源创建图形路径。

[C#]

using (var image = (TiffImage)Image.Load("Bottle.tif"))
{
    // 使用来自 TIFF 图像的 PathResources 创建 GraphicsPath
    var graphicsPath = PathResourceConverter.ToGraphicsPath(image.ActiveFrame.PathResources.ToArray(), image.ActiveFrame.Size);
    var graphics = new Graphics(image);

    // 画红线并保存图片
    graphics.DrawPath(new Pen(Color.Red, 10), graphicsPath);
    image.Save("BottleWithRedBorder.tif");
}

使用图形路径创建路径资源。

[C#]

static void Main(string[] args)
{
    using (var image = (TiffImage)Image.Load("Bottle.tif"))
    {
        // 为 GraphicsPath 创建矩形图形
        var figure = new Figure();
        figure.AddShape(CreateBezierShape(100f, 100f, 500f, 100f, 500f, 1000f, 100f, 1000f));

        // 使用我们的 Figure 创建 GraphicsPath
        var graphicsPath = new GraphicsPath();
        graphicsPath.AddFigure(figure);

        // 使用 GraphicsPath 设置 PathResources
        var pathResouze = PathResourceConverter.FromGraphicsPath(graphicsPath, image.Size);
        image.ActiveFrame.PathResources = new List<PathResource>(pathResouze);

        // 保存图片
        image.Save("BottleWithRectanglePath.tif");
    }
}

private static BezierShape CreateBezierShape(params float[] coordinates)
{
    var bezierPoints = CoordinatesToBezierPoints(coordinates).ToArray();
    return new BezierShape(bezierPoints, true);
}

private static IEnumerable<PointF> CoordinatesToBezierPoints(float[] coordinates)
{
    for (var coordinateIndex = 0; coordinateIndex < coordinates.Length; coordinateIndex += 2)
        for (var index = 0; index < 3; index++)
            yield return new PointF(coordinates[coordinateIndex], coordinates[coordinateIndex + 1]);
}

也可以看看