---
title: "Subtract vector paths to cut a hole in C#"
url: https://recursiveai.org/oneimage/cookbook/subtract-vector-paths-to-cut-a-hole/
description: "Subtract one closed vector path from another in C#. Fill the remaining shape on a transparent canvas and save a PNG with a visible cutout."
version: "1.11.2"
---

# Subtract vector paths to cut a hole in C#

**OneImage** is a free, open-source image library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneImage) · [GitHub](https://github.com/recursive-ai-oss/oneimage-csharp) · [Docs](https://recursiveai.org/oneimage/docs/) · [Download example (.zip)](https://recursiveai.org/oneimage/cookbook/subtract-vector-paths-to-cut-a-hole/subtract-vector-paths-to-cut-a-hole.zip)

Subtract the inner shape before filling the outer one, so the hole stays transparent.

```shell
dotnet add package RecursiveAI.OneImage
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneImage;
using OneImage.Drawing;
using Path = OneImage.Drawing.Path;
var outer = Path.Circle(80, 80, 64);
var inner = Path.Circle(80, 80, 32);
var blank = Image.FromPixels<Rgba32>(
    new Rgba32[160 * 160], 160, 160);
blank.Fill(new Rgba32(30, 110, 220, 255),
    outer.Subtract(inner)).Save("ring.png");
```

[The example](https://recursiveai.org/oneimage/cookbook/subtract-vector-paths-to-cut-a-hole/subtract-vector-paths-to-cut-a-hole.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

Writes `ring.png`, a blue ring on a transparent 160 × 160 image. No input files are needed.

## The full program

```csharp
using OneImage;
using OneImage.Drawing;
using Path = OneImage.Drawing.Path;

var outer = Path.Circle(80, 80, 64);
var inner = Path.Circle(80, 80, 32);
var ring = outer.Subtract(inner);
var ink = new Rgba32(30, 110, 220, 255);
var blank = Image.FromPixels<Rgba32>(
    new Rgba32[160 * 160], 160, 160);
blank.Fill(ink, ring).Save("ring.png");

Image saved = Image.Load("ring.png");
var pixels = saved.ToArray<Rgba32>();
Console.WriteLine($"ring.png: {saved.Width}x{saved.Height}");
Console.WriteLine($"Hole alpha: {pixels[80 * 160 + 80].A}");
Console.WriteLine($"Ring alpha: {pixels[80 * 160 + 32].A}");
```

```text
ring.png: 160x160
Hole alpha: 0
Ring alpha: 255
```

![Blue ring with a transparent center](https://recursiveai.org/oneimage/images/subtract-vector-paths-to-cut-a-hole/ring.png)

The program reloads the PNG and reads two pixels away from the edges: the center is fully transparent (`0`) and the left side of the ring is opaque (`255`). If the inner shape covers the whole outer shape, `Subtract` returns `Path.Empty` and there’s nothing to paint.

## Variations

**Cut a notch instead of a hole.** With `outer` and `blank` from the snippet at the top, put the opening over the right edge. Only the part that overlaps the outer circle is removed:

```csharp
var notch = outer.Subtract(Path.Circle(140, 80, 32));
blank.Fill(new Rgba32(30, 110, 220, 255),
    notch).Save("notch.png");
```

## Related recipes

- [Draw a vector path on an image](https://recursiveai.org/oneimage/cookbook/draw-vector-path-on-image/)
- [Apply an alpha mask to an image](https://recursiveai.org/oneimage/cookbook/apply-alpha-mask-to-image/)

## The short answer

Use `outer.Subtract(inner)` to subtract one closed vector path from another in C#. It returns the part of `outer` that isn’t covered by `inner`. Fill that path on a transparent RGBA image and save it as PNG to leave the opening transparent in the output.

## How do you cut a hole in a vector shape?

Make two closed paths in the same coordinate system, then subtract the shape of the opening from the one you want to keep. In the example both paths are circles centered at `(80, 80)`. The opening has radius 32 and the outer boundary has radius 64, so a band 32 pixels thick remains between them. You can use a different shape for the opening; the inside path doesn’t need to match the outside. What matters is that the opening lies wholly inside the boundary if you want a closed hole rather than a notch.

This operation is called a **Boolean difference**. Order matters: `inner.Subtract(outer)` in this example is empty because all of the smaller circle is covered by the larger one. If the two paths don’t overlap, the filled region of the first path is unchanged.

The [drawing guide](https://recursiveai.org/oneimage/docs/drawing/#path-booleans) describes the result as “a `Path` of straight-edged, closed polygons.” Curves are flattened to short line segments using the path operation’s tolerance (0.1 pixel by default). The resulting outer contours and holes run in opposite directions, so the result fills the same under the non-zero and even-odd fill rules. You don’t have to reverse the inner circle yourself.

## How do you make the cutout transparent in a PNG?

Start with a transparent background, not a white one. In the example the new `Rgba32` array has zero in every channel, including alpha. Filling the difference paints the band but doesn’t paint the opening. Where the path crosses a pixel, anti-aliasing can leave it partly transparent, so use pixels well away from an edge if you want to check that the hole is completely clear.

Subtracting paths changes geometry, not pixels that are already on a canvas. If you fill the ring on top of a photo, you’ll see the photo through the hole; you won’t erase the photo. To cut a hole in existing pixels, use an alpha mask instead. To keep an editable outline, keep the resulting `Path`; saving to PNG turns it into pixels at the chosen size.
