Conditionally adding pipeline elements

Pipeline Resize Advanced Thumbnail

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.

Сode Snippet

// 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();
}

Input

Venice.jpg

Output

thumbnail_in_srgb.jpg

For AI-assisted development: Download Graphics Mill Code Samples XML Catalog