How to create Graphicspath from PathResource (C# .NET)

Hi,

how can I create a GraphicsPath from PathResource and vice versa?

Best Regards
Yahia

@it-yeq,

I have tried understanding your requirements and unable to completely understand them. Can you please share the details of requirements in the form of sample image and image format you require.

Hi,

When loading a TIFF or PSD there is a property called PathResource on the Frame.
This path resource represent a vector path (meant for clipping).
I would like to use such a path to clip the image and I would like to be able to create such a path and save it to the image.

One example TIFF with such a path is attached.

How can I implement this with Aspose.Imaging?

Best Regards
YahiaTest4Path.zip (1.2 MB)

@it-yeq,

We have recently introduced feature in Aspose.Imaging for .NET 20.5 for support for clipping paths.

Access Clipping Paths in TIFF image

PathResources property allows you to access Clipping Paths in TIFF frame. The following code retrieves paths from TIFF image and displays their names in the console:

using (var image = (TiffImage)Image.Load("Sample.tif"))
{
    foreach (var path in image.ActiveFrame.PathResources)
    {
        Console.WriteLine(path.Name);
    }
}

Modify existing Clipping Paths

You can easily modify already existing Clipping Paths. For instance, you can keep only one Clipping Path in the image:

<code>
using (var image = (TiffImage)Image.Load("Sample.tif"))
{
    var paths = image.ActiveFrame.PathResources;
    image.ActiveFrame.PathResources = paths.Take(1).ToList();
    image.Save();
}
</code>

Create Clipping Path manually

You can manually create Clipping Path in TIFF image. In order to do that you need to create an instance of PathResource class. The following code demonstrates the way how you can create an empty path in TIFF image:

<code>
var options = new TiffOptions(TiffExpectedFormat.Default);
var frame = new TiffFrame(options, 800, 600);

using (var image = new TiffImage(frame))
{
    image.ActiveFrame.PathResources = new List<PathResource>
    {
        new PathResource
        {
            BlockId = 2000,
            Name = "My Clipping Path",
            Records = new List<VectorPathRecord>()
        }
    };

    image.Save("ImageWithEmptyPath.tiff");
}
</code>

Can you please consider exploring the above features on your end using latest Aspose.Imaging 20.5 on your end.

Hi,

thank you very much… one point is missing: how do I use the clipping path to clip an image?
Accessing the name is fine… but to clip the image I must convert the Records to something the can be used when rendering the image (like a GraphicsPath object or an alpha channel or…).

How can I do that?

Best Regards
Yahia

@it-yeq,

To create your own Clipping Paths you need to understand their content. Photoshop stores its paths as resources with IDs in the range 2000 through 2997. The name of the resource is the name given to the path when it was saved. If the file contains a resource with an ID of 2999, then this resource contains the name of the clipping path. Each path has a set of records to hold the data.

Record classes:
LengthRecord - contains the number of Bezier knot records.
BezierKnotRecord - describes the knots of the path.
ClipboardRecord - contains four fixed-point numbers for the bounding rectangle.

More details you can find in Adobe Photoshop File Formats Specification.

Thank you very much - I understand.
These records by themselves are not directly usable for rendering, filling etc.
To use them in such ways we need to create for example Aspose.Imaging.GraphicsPath (or System.Drawing.Drawing2D.GraphicsPath… ) based on those records.

The question is basically: Is there anything in Aspose.Imaging that can help with that?

Best Regards
Yahia

@it-yeq,

Can you please share how Aspose.Imaging may further assist you in this.

For example:
Is there a constructor for Aspose.Imaging.GraphicsPath that takes List of VectorPathRecord as parameter?
Is there a metod in Aspose.Imaging.GraphicsPath that returns List of VectorPathRecord as result?

Best Regards
Yahia

@it-yeq,

We need to further investigate the possibility of requested information on our and for that I have associated information with ticket IMAGINGNET-3731. We will share the further feedback with you as soon as the requirement will be addressed.

Hi,

any news on this?

Best Regards
Yahia

@it-yeq,

Clipping Path

Clipping path is the Photoshop technique to remove the background from an image. Photoshop allows you to select a part of an image using Clipping Path and save the path within a file. Clipping Paths allow you to hide the part of an image you don’t want to appear. Anything inside the clipping path will be visible, but anything outside of it will be transparent.

Other words Photoshop makes it possible to isolate certain parts of an image, without permanently changing the layer. This allows you to tweak the image at any point in the creative process. Clipping Paths are a traditional method of cutting out objects or people in Photoshop that allows you to create image files with transparent backgrounds. This approach works best with objects or people with “hard” edges around the object or person you want to cut out.

Access Clipping Paths in TIFF image

PathResources property allows you to access Clipping Paths in TIFF frame. The following code retrieves paths from TIFF image and displays their names in the console:

using (var image = (TiffImage)Image.Load("Sample.tif"))
{
    foreach (var path in image.ActiveFrame.PathResources)
    {
        Console.WriteLine(path.Name);
    }
}

Transfer Clipping Paths during export from TIFF to PSD image

Its quite helpful to use Clipping Paths in PSD images. You can easily transfer your Clipping Paths to PSD image using the following code:

using (var image = Image.Load("Sample.tif"))
{
    image.Save("SampleWithPaths.psd", new PsdOptions());
}

Modify existing Clipping Paths

You can easily modify already existing Clipping Paths. For instance, you can keep only one Clipping Path in the image:

using (var image = (TiffImage)Image.Load("Sample.tif"))
{
    var paths = image.ActiveFrame.PathResources;
    image.ActiveFrame.PathResources = paths.Take(1).ToList();
    image.Save();
}

Create Clipping Path manually

You can manually create Clipping Path in TIFF image. In order to do that you need to create an instance of PathResource class. The following code demonstrates the way how you can create a path in TIFF image:

static void Main()
{
    using (var image = (TiffImage)Image.Load("Sample.tif"))
    {
        image.ActiveFrame.PathResources = new List<PathResource> { new PathResource
        {
            BlockId = 2000,                                                          // Block Id according to Photoshop specification
            Name = "My Clipping Path",                                               // Path name
            Records = CreateRecords(0.2f, 0.2f, 0.8f, 0.2f, 0.8f, 0.8f, 0.2f, 0.8f)  // Create path records using coordinates
        }};

        image.Save("ImageWithPath.tif");
    }
}

private static List<VectorPathRecord> CreateRecords(params float[] coordinates)
{
    var records = CreateBezierRecords(coordinates);                                  // Create Bezier records using coordinates

    records.Insert(0, new LengthRecord                                               // LengthRecord required by Photoshop specification
    {
        IsOpen = false,                                                              // Lets create closed path
        RecordCount = (ushort)records.Count                                          // Record count in the path
    });

    return records;
}

private static List<VectorPathRecord> CreateBezierRecords(float[] coordinates)
{
    return CoordinatesToPoints(coordinates)
        .Select(CreateBezierRecord)
        .ToList();
}

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

private static VectorPathRecord CreateBezierRecord(PointF point)
{
    return new BezierKnotRecord { PathPoints = new[] { point, point, point } };
}

Clipping Path content

To create your own Clipping Paths you need to understand their content. Photoshop stores its paths as resources with IDs in the range 2000 through 2997. The name of the resource is the name given to the path when it was saved. If the file contains a resource with an ID of 2999, then this resource contains the name of the clipping path. Each path has a set of records to hold the data.

Record classes:
LengthRecord - contains the number of Bezier knot records.
BezierKnotRecord - describes the knots of the path.
ClipboardRecord - contains four fixed-point numbers for the bounding rectangle.

More details you can find in Adobe Photoshop File Formats Specification.

Please download File from here.

Hi,

thank you… but this does not help really with the goal:
To be able to use the clipping path with image processing I need to create a GraphicsPath object based on the PathResource etc.
How do I go about this? Is there anything in Aspose.Imaging that actually helps with such a taks?

Best Regards
Yahia

@it-yeq,

The graphics path is not supported at the moment. A ticket with ID IMAGINGNET-3989 has been created as new feature request to provide the requested support. We will share the the good news with you as soon as the issue will be fixed.

1 Like

The issues you have found earlier (filed as IMAGINGNET-3989) have been fixed in this update.