---
title: "Combine shapes with boolean path operations"
canonical: https://www.graphicsmill.com/samples/Drawing/Paths/CombinePaths_cs
product: "Graphics Mill"
source: "Drawing/Paths/CombinePaths.cs"
---

# Combine shapes with boolean path operations

This sample shows how to build new shapes from existing ones with the boolean operations of the ` Path ` class: ` Path.Union `, ` Path.Intersect `, ` Path.Subtract `, and ` Path.Xor `. These static methods leave their arguments unchanged and return a new path, so the same two source shapes can be combined in several ways. The result is real vector geometry, so it can be filled, stroked, written to PDF, or used as a clipping path.

## Instruction

Use the drawing API to combine two overlapping shapes, a circle and a square, with Path.Union, Path.Intersect, Path.Subtract, and Path.Xor, and draw the four results side by side with captions.

## Usings

```csharp
using Aurigma.GraphicsMill.Drawing;
```

## Code Snippet

```csharp
var square = Path.Rectangle(20, 20, 120, 120);
var circle = Path.Ellipse(70, 70, 120, 120);

var operations = new (string Name, Path Result)[]
{
    ("Union", Path.Union(square, circle)),
    ("Intersect", Path.Intersect(square, circle)),
    ("Subtract", Path.Subtract(square, circle)),
    ("Xor", Path.Xor(square, circle)),
};

const float cellWidth = 220;

using (var bitmap = new Bitmap((int)cellWidth * operations.Length, 250, PixelFormat.Format24bppRgb, RgbColor.White))
using (var graphics = bitmap.GetGraphics())
{
    var font = graphics.CreateFont("Arial", 18f);

    for (int i = 0; i < operations.Length; i++)
    {
        float offsetX = i * cellWidth;

        // The operations return new paths, so the source shapes can be reused for every cell.
        var result = new Path(operations[i].Result);
        result.Translate(offsetX, 0);

        var sourceSquare = new Path(square);
        sourceSquare.Translate(offsetX, 0);

        var sourceCircle = new Path(circle);
        sourceCircle.Translate(offsetX, 0);

        graphics.FillPath(new SolidBrush(new RgbColor(46, 134, 222)), result);
        graphics.DrawPath(new Pen(RgbColor.Gray, 1f), sourceSquare);
        graphics.DrawPath(new Pen(RgbColor.Gray, 1f), sourceCircle);

        var caption = new PlainText(operations[i].Name, font)
        {
            Position = new System.Drawing.PointF(offsetX + 105, 230),
            Alignment = TextAlignment.Center,
        };

        graphics.DrawText(caption);
    }

    bitmap.Save("CombinePaths.png");
}
```

## Explanation

The code:

- Creates a square with ` Path.Rectangle ` and an overlapping circle with ` Path.Ellipse `.
- Combines them with ` Path.Union `, ` Path.Intersect `, ` Path.Subtract `, and ` Path.Xor `; each call returns a new ` Path `.
- For each result, moves a copy into its own cell with ` Path.Translate `, draws the source shapes as thin grey outlines, fills the result, and writes a caption with ` PlainText `.
- Saves the image as "CombinePaths.png".

## Tags

- [Bitmap](<https://www.graphicsmill.com/samples/tags/bitmap_md>): Working with images via the Bitmap API
- [Drawing](<https://www.graphicsmill.com/samples/tags/drawing_md>): Drawing vector shapes and text
- [Path](<https://www.graphicsmill.com/samples/tags/path_md>): Building, offsetting and combining vector paths

## Output

- [CombinePaths.png](<https://www.graphicsmill.com/samples/Drawing/compiled/Debug/CombinePaths_png>) (preview)

## Used APIs

- [Aurigma.GraphicsMill.Bitmap.GetGraphics](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Bitmap_GetGraphics.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Graphics.CreateFont(String,Single)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_CreateFont_2_1dbfa9fa.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Graphics.DrawPath(Pen,Path)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_DrawPath_2_77634c23.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Graphics.DrawText(Text)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_DrawText_1_f576d03f.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Graphics.FillPath(Brush,Path)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_FillPath_2_3cee1596.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Path](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_Path.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Path.Ellipse(Single,Single,Single,Single)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Path_Ellipse_4_37f6c741.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Path.Intersect(Path,Path)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Path_Intersect_2_87697a01.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Path.Rectangle(Single,Single,Single,Single)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Path_Rectangle_4_37f6c741.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Path.Subtract(Path,Path)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Path_Subtract_2_87697a01.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Path.Translate(Single,Single)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Path_Translate_2_427c5821.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Path.Union(Path,Path)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Path_Union_2_87697a01.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Path.Xor(Path,Path)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Path_Xor_2_87697a01.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Pen](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_Pen.htm.md>)
- [Aurigma.GraphicsMill.Drawing.PlainText](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_PlainText.htm.md>)
- [Aurigma.GraphicsMill.Drawing.PlainText.Alignment](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_PlainText_Alignment.htm.md>)
- [Aurigma.GraphicsMill.Drawing.PlainText.Position](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_PlainText_Position.htm.md>)
- [Aurigma.GraphicsMill.Drawing.SolidBrush](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_SolidBrush.htm.md>)
- [Aurigma.GraphicsMill.Drawing.TextAlignment](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_TextAlignment.htm.md>)
