@it-yeq,
In order to address the above requirements, I suggest you to please go through following example and explanation. Please use latest Aspose.Imaging for .NET 20.
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.
In 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
In order 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.
For more details you can find in Adobe Photoshop File Formats Specification.
Can you please share the details and explanation regarding this as we are unable to understand it. It would be good, if you may please share a sample example.