Exemplo n.º 1
0
		static void ClipObjectTo(GroupObject groupObject, Bounds boundingBox)
		{
			List<PathTriple> triples = new List<PathTriple>()
				{
					new PathTriple(boundingBox.TopLeft),
					new PathTriple(boundingBox.TopRight),
					new PathTriple(boundingBox.BottomRight),
					new PathTriple(boundingBox.BottomLeft)
				};
			PathShape clipPathShape = new PathShape();
			clipPathShape.AddPath(new Path(triples)
			{
				IsClosed = true
			});
			// clipPathShape is now a rectangle with coordinates matching our bounding box.

			// We turn that into a ShapeObject
			ShapeObject clipShape = new ShapeObject();
			clipShape.Shape = clipPathShape;

			// Add it to the group object and turn on clipping.
			groupObject.IsClipped = true;
			groupObject.AddObject(clipShape);
		}
Exemplo n.º 2
0
		private static void DistressMethod2()
		{
			VectorDesign design = VectorDesign.Load(@"DistressDesign.PV", VectorDesignFileTypes.Pv);
			design.Render(@"DistressDesign-Method2-Original.PNG", RenderFormats.Png, 300, design.ColourContext);

			// Distress method #2 is similar to method #1, 
			// however, in this case:
			// 1. Create a distress pattern. This must be a vector design.
			//    It is simply white "random" objects on a transparent background.
			// 2. Combine the distress objects with a bounding rectangle to "invert" them from
			//    a bunch of filled objects, to a bunch of holes in a rectangle.
			// 3. Use the new distress object as a clipping mask over the design.
			//    Some scaling of the distress pattern may be necessary to make it look good.
			// 4. Rendering the merged design.
			//
			// Unlike method #1, this method will work when rendering onto a transparent background.
			
			var designBounds = design.GetBounds();

			
			VectorDesign distress = VectorDesign.Load(@"DistressPattern.PV", VectorDesignFileTypes.Pv);
			var distressBounds = distress.GetBounds();

			// Create a new rectangle shape into which we'll add the distress shapes as "holes".
			Path rectangle = new Path();
			rectangle.Triples.Add(new PathTriple(distressBounds.TopLeft));
			rectangle.Triples.Add(new PathTriple(distressBounds.TopRight));
			rectangle.Triples.Add(new PathTriple(distressBounds.BottomRight));
			rectangle.Triples.Add(new PathTriple(distressBounds.BottomLeft));
			rectangle.IsClosed = true;

			var distressShape = new PathShape();
			distressShape.AddPath(rectangle);

			// Take all the objects from the distress design
			// and copy them to our PathShape instead.
			var distressFirstPage = distress.Pages.First();
			var distressFirstLayer = distressFirstPage.Layers.First();

			foreach (var shapeObject in distress.ShapeObjects)
			{
				PathShape pathShape = shapeObject.Shape as PathShape;
				if (pathShape != null)
				{
					distressShape.AddPaths(pathShape.Paths);
				}
			}

			// At this point, distressShape should be a rectangle with a bunch of "holes" in it.

			// Move the distress so it's center is at the same location as the design's center
			var translate = Matrix3x2.Translate(designBounds.Center.X - distressBounds.Center.X, designBounds.Center.Y - distressBounds.Center.Y);
			distressShape.ApplyTransform(translate);

			// Depending on the distress, you may need to scale it.
			// Calculate an appropriate scaling factor based on your use case.
			//var scale = Matrix3x2.Scale(...);
			//distressShape.ApplyTransform(scale);

			// This will be our clipping mask
			ShapeObject distressObject = new ShapeObject();
			distressObject.Shape = distressShape;
			//distressObject.Fill = new SolidFill(new RGBColour(0, 0, 0));

			// Create a new design to act as our resulting "distressed" design
			VectorDesign result = new VectorDesign();
			result.ColourContext = design.ColourContext;
			design.ResourceManager.CopyContentsTo(result.ResourceManager);
			var resultPage = result.NewPage();
			var resultLayer = resultPage.NewLayer();

			// Copy all items from the source design to our new group object
			GroupObject newGroup = new GroupObject();

			design.ProcessObjects(obj => 
				{
					newGroup.AddObject(obj);
					return obj;
				});

			// Add the clipping mask to the group as our clipping mask
			newGroup.AddObject(distressObject);
			newGroup.IsClipped = true;
			
			resultLayer.AddObject(newGroup);
			

			// Render on a white background
			result.Render(@"DistressDesign-Method2-Final.PNG", RenderFormats.Png, 300, design.ColourContext);
		}