---
title: "Conditionally adding pipeline elements"
canonical: https://www.graphicsmill.com/samples/LargeImages/PipelineAPISyntax/ConditionalElements_cs
product: "Graphics Mill"
source: "LargeImages/PipelineAPISyntax/ConditionalElements.cs"
---

# Conditionally adding pipeline elements

This sample demonstrates how to conditionally add elements to a pipeline based on the properties of the input image.
It shows how to create thumbnails in the sRGB color space with a specified DPI value.  
  

You can read more information about pipelines in the [documentation](<https://www.graphicsmill.com/docs/gm/processing-large-images-using-pipelines.htm>).

## Instruction

Create thumbnail from the "Venice.jpg" image, converting to sRGB color space if necessary. Set the DPI to 72. Save result to "thumbnail_in_srgb.jpg".

## Usings

```csharp
using Aurigma.GraphicsMill.Codecs;
using Aurigma.GraphicsMill.Transforms;
```

## Code Snippet

```csharp
// Create an empty pipeline.
var pipeline = new Pipeline();

// Add a reader as the first element.
var reader = ImageReader.Create("Venice.jpg");
pipeline.Add(reader);

// Add a resize transform.
pipeline.Add(new Resize(320, 0, ResizeInterpolationMode.High));

// Assume that we want thumbnails only in RGB format.
// The order here affects performance because a color converter placed after the down-resizer has less data to process.
if (reader.PixelFormat != PixelFormat.Format24bppRgb)
{
    var colorConverter = new ColorConverter(PixelFormat.Format24bppRgb)
    {
        DestinationProfile = ColorProfile.FromSrgb(),
    };

    pipeline.Add(colorConverter);
}

// We also want the DPI to be equal to 72.
if (reader.DpiX != 72 || reader.DpiY != 72)
{
    pipeline.Add(new ResolutionModifier(72, 72));
}

// Add the JPEG writer.
pipeline.Add(ImageWriter.Create("thumbnail_in_srgb.jpg"));

try
{
    // Run the pipeline.
    pipeline.Run();
}
finally
{
    // Dispose all elements in the pipeline.
    pipeline.DisposeAllElements();
}
```

## Explanation

The code:

- Reads the image.
- Creates an empty pipeline.
- Adds ImageReader as the first element.
- Adds a resize transform.
- Conditionally adds a color converter if the image is not in sRGB color space.
- Conditionally adds a resolution modifier if the DPI is not 72.
- Adds ImageWriter as the last element.
- Runs the pipeline and disposes all elements.

## Tags

- [Advanced](<https://www.graphicsmill.com/samples/tags/advanced_md>): Demonstrating advanced usage scenarios
- [Pipeline](<https://www.graphicsmill.com/samples/tags/pipeline_md>): Working with images via the Pipeline API
- [Resize](<https://www.graphicsmill.com/samples/tags/resize_md>): Resizing images
- [Thumbnail](<https://www.graphicsmill.com/samples/tags/thumbnail_md>): Generating thumbnails from images

## Input

- [Venice.jpg](<https://www.graphicsmill.com/samples/LargeImages/compiled/Debug/Venice_jpg>) (preview)

## Output

- [thumbnail\_in\_srgb.jpg](<https://www.graphicsmill.com/samples/LargeImages/compiled/Debug/thumbnail_in_srgb_jpg>) (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.ImageReader.DpiX](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_ImageReader_DpiX.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ImageReader.DpiY](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_ImageReader_DpiY.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ImageReader.PixelFormat](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_ImageReader_PixelFormat.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ImageWriter](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_ImageWriter.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ImageWriter.Create(String)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_ImageWriter_Create_1_bb3a7a4f.htm.md>)
- [Aurigma.GraphicsMill.ColorProfile](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_ColorProfile.htm.md>)
- [Aurigma.GraphicsMill.ColorProfile.FromSrgb](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_ColorProfile_FromSrgb.htm.md>)
- [Aurigma.GraphicsMill.Transforms.ColorConverter](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Transforms_ColorConverter.htm.md>)
- [Aurigma.GraphicsMill.Transforms.ColorConverter.DestinationProfile](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Transforms_ColorConverter_DestinationProfile.htm.md>)
- [Aurigma.GraphicsMill.Transforms.Resize](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Transforms_Resize.htm.md>)
- [Aurigma.GraphicsMill.Transforms.ResizeInterpolationMode](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Transforms_ResizeInterpolationMode.htm.md>)
- [Aurigma.GraphicsMill.Transforms.ResolutionModifier](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Transforms_ResolutionModifier.htm.md>)
