---
title: "Remove a gradient background from a product photo"
canonical: https://www.graphicsmill.com/samples/Transforms/BackgroundRemoval/RemoveContiguousBackgroundMemoryFriendly_cs
product: "Graphics Mill"
source: "Transforms/BackgroundRemoval/RemoveContiguousBackgroundMemoryFriendly.cs"
---

# Remove a gradient background from a product photo

This sample shows how to cut a product out of a studio shot whose background is not a flat colour but a soft gradient, using the ` ContiguousBackgroundRemoval ` transform in a pipeline. ` Tolerance ` limits how far a background pixel may be from the background colour, while ` StepTolerance ` lets the background change gradually from pixel to pixel and stops at the sharp edge of the product. Colour differences are measured in CIE L*a*b* (delta E), so the values follow what the eye sees. The pipeline keeps memory use bounded, so the same code works for very large images.

## Instruction

Use the Pipeline API to remove the light gradient background of a product photo with the ContiguousBackgroundRemoval transform, tuning its Tolerance and StepTolerance properties, and write the result with transparency to PNG.

## Usings

```csharp
using Aurigma.GraphicsMill.Codecs;
using Aurigma.GraphicsMill.Transforms;
```

## Code Snippet

```csharp
using (var reader = ImageReader.Create("Apple.jpg"))
using (var removal = new ContiguousBackgroundRemoval())
using (var writer = new PngWriter("RemoveContiguousBackgroundMemoryFriendly.png"))
{
    // The backdrop gets darker towards the bottom: allow a larger total difference from the
    // background colour, but only small steps between neighbouring pixels.
    removal.Tolerance = 30;
    removal.StepTolerance = 4;

    Pipeline.Run(reader + removal + writer);
}
```

## Explanation

The code:

- Creates an ` ImageReader ` for the product photo "Apple.jpg".
- Creates a ` ContiguousBackgroundRemoval ` transform and leaves ` BackgroundColor ` unset, so it is taken from the border of the image.
- Sets ` Tolerance ` (the largest delta E from the background colour) and ` StepTolerance ` (the largest delta E between neighbouring background pixels), so that the shaded parts of the backdrop are still background while the edge of the apple stops the fill.
- Creates a ` PngWriter ` for "RemoveContiguousBackgroundMemoryFriendly.png", which keeps the new alpha channel.
- Runs the reader, the transform, and the writer as one pipeline.

## Tags

- [Background Removal](<https://www.graphicsmill.com/samples/tags/background-removal_md>): Making the background around a subject transparent
- [Pipeline](<https://www.graphicsmill.com/samples/tags/pipeline_md>): Working with images via the Pipeline API
- [Transform](<https://www.graphicsmill.com/samples/tags/transform_md>): Image transforms and effects

## Input

- [Apple.jpg](<https://www.graphicsmill.com/samples/Transforms/compiled/Debug/Apple_jpg>) (preview)

## Output

- [RemoveContiguousBackgroundMemoryFriendly.png](<https://www.graphicsmill.com/samples/Transforms/compiled/Debug/RemoveContiguousBackgroundMemoryFriendly_png>) (preview)

## Used APIs

- [Aurigma.GraphicsMill.Codecs.ImageReader](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_ImageReader.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ImageReader.Create(String)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_ImageReader_Create_1_bb3a7a4f.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PngWriter](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PngWriter.htm.md>)
- [Aurigma.GraphicsMill.Transforms.ContiguousBackgroundRemoval](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Transforms_ContiguousBackgroundRemoval.htm.md>)
- [Aurigma.GraphicsMill.Transforms.ContiguousBackgroundRemoval.StepTolerance](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Transforms_ContiguousBackgroundRemoval_StepTolerance.htm.md>)
- [Aurigma.GraphicsMill.Transforms.ContiguousBackgroundRemoval.Tolerance](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Transforms_ContiguousBackgroundRemoval_Tolerance.htm.md>)
