This forum contains outdated content and is available for reading only. Please contact technical support if you have any questions.

Clipping path doesn't adapt using bitmap

Hello,

Is it possible to adapth the clipping path when rotating or cropping an image with bitmap? I've tried with the pipeline and it worked, however when I try with bitmap, the clipping path doesn't adapt.


Code snippet working (pipeline):
public byte[] Test(byte[] media)
{
	using (var imageStream = new MemoryStream(media))
	using (var exportedImageStream = new MemoryStream())
	using (var reader = new JpegReader(imageStream))
	using (var writer = new JpegWriter(exportedImageStream))
	using (var rotate = new Rotate(60f))
	using (var crop = new Crop(reader.Width / 2, 0, reader.Width / 2, reader.Height / 2))
	{
		writer.AdobeResources = reader.AdobeResources;

		Pipeline.Run(reader + rotate + crop + writer);

		return exportedImageStream.GetBuffer();
	}
}



Code snippet not working (bitmap):
public byte[] Test(byte[] media, int width, int height)
{
	using (var imageStream = new MemoryStream(media))
	using (var exportedImageStream = new MemoryStream())
	using (var imageReader = ImageReader.Create(imageStream))
	using (var bitmap = imageReader.Frames[0].GetBitmap())
	{
		var jpegSettings = new JpegSettings { Quality = 100 };

		#region metadata

		if (imageReader.AdobeResources != null)
		{
			jpegSettings.AdobeResources = imageReader.AdobeResources;
		}

		if (imageReader.Exif != null)
		{
			jpegSettings.Exif = imageReader.Exif;
		}

		if (imageReader.Iptc != null)
		{
			jpegSettings.Iptc = imageReader.Iptc;
		}

		if (imageReader.Xmp != null)
		{
			jpegSettings.Xmp = imageReader.Xmp;
		}

		#endregion

		bitmap.Transforms.Resize(width, height);
		bitmap.Transforms.Crop(imageReader.Width / 2, 0, imageReader.Width / 2, imageReader.Height / 2);

		bitmap.Transforms.Rotate(30f);

		bitmap.Save(exportedImageStream, jpegSettings);

		return exportedImageStream.GetBuffer();
	}
}


Thank you in advance,
Kind regards

Attachments

The clipping path is processed correctly using the Pipeline API only.

Is this limitation critical for you?
This limitation it's not critical, but I would like to have this feature in the future, if it's possible...

Best regards
I have added the feature to the backlog of Graphics Mill (Aurigma Bug #0021662).
We have just released the new version 8.1.12 of the Graphics Mill SDK where we have implemented manipulation of clipping path.

F.Metadata\F.05.ClippingPath\ClippingPathExample.cs

/// <summary>
/// Modifies clipping path explicitly using memory-friendly Pipeline API
/// </summary>
private static void ModifyClippingPathExplicitlyMemoryFriendly()
{
	int width = 1000;
	int height = 1000;

	using (var reader = new JpegReader(@"../../../../_Input/Apple.jpg"))
	using (var generator = new ImageGenerator(width, height, reader.PixelFormat, RgbColor.White))
	using (var combiner = new Combiner(CombineMode.Copy))
	using (var writer = new JpegWriter(@"../../../../_Output/ModifyClippingPathExplicitlyMemoryFriendly.jpg"))
	{
		combiner.TopImage = reader;
		combiner.X = (width - reader.Width) / 2;
		combiner.Y = (height - reader.Height) / 2;

		//The clipping path has relatives coordinates (0.0f ... 1.f0). So we need to transform it.
		var transform = new System.Drawing.Drawing2D.Matrix();
		transform.Scale((float)reader.Width / (float)width, (float)reader.Height / (float)height);
		transform.Translate((float)combiner.X / (float)reader.Width, (float)combiner.Y / (float)reader.Height);

		var adobeResources = reader.AdobeResources;

		//2000-2997 Clipping path information 
		//http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/ 
		const int firstPathId = 2000;
		const int lastPathId = 2997;

		//Remove clipping paths
		foreach (long key in adobeResources.Keys)
		{
			if (key >= firstPathId && key <= lastPathId)
			{
				adobeResources.Remove(key);
			}
		}

		//Transform and save clipping paths
		for (var i = 0; i < reader.ClippingPaths.Count; i++)
		{
			var clippingPath = reader.ClippingPaths[0];
			clippingPath.ApplyTransform(transform);

			adobeResources.Add(2000 + i, new AdobeResourceBlock(clippingPath.Name, clippingPath.Data));
		}

		writer.AdobeResources = adobeResources;

		Pipeline.Run(generator + combiner + writer);
	}
}


/// <summary>
/// Modifies clipping path explicitly
/// </summary>
private static void ModifyClippingPathExplicitly()
{
	using (var reader = new JpegReader(@"../../../../_Input/Apple.jpg"))
	using (var bitmap = reader.Frames[0].GetBitmap())
	{
		var crop = new Crop(20, 20, bitmap.Width - 40, bitmap.Height - 40);

		var cropped = crop.Apply(bitmap);

		var clippingPath = reader.ClippingPaths[0];
		
		clippingPath.ApplyTransform(crop.GetPathTransformMatrix(bitmap.Width, bitmap.Height).ToGdiPlusMatrix());

		var adobeResources = new AdobeResourceDictionary();
		adobeResources.Add(2000, new AdobeResourceBlock("Apple", clippingPath.Data));

		var jpegSettings = new JpegSettings();
		jpegSettings.AdobeResources = adobeResources;

		cropped.Save(@"../../../../_Output/ModifyClippingPathExplicitly.jpg", jpegSettings);
	}
}