---
title: "Using the OnInit event to configure pipeline elements"
canonical: https://www.graphicsmill.com/samples/LargeImages/PipelineAPISyntax/OnInitEvent_cs
product: "Graphics Mill"
source: "LargeImages/PipelineAPISyntax/OnInitEvent.cs"
---

# Using the OnInit event to configure pipeline elements

What if we want to adjust pipeline element settings depending on the actual input data? In most cases you have access to the source element, but in more sophisticated scenarios the pipeline may be large, and it may be hard to track all possible changes of the image parameters. Or you may decide to split the pipeline assembly logic into several methods.
In this case, it is possible to change pipeline element settings from an OnInit event handler. You get the actual image parameters that the element receives from its source, and change the properties according to those parameters.
In this example, we'll scale the original image by a factor of 2.5 while preserving its aspect ratio, and place a watermark image in the center. The watermark image will be half the size of the bottom image.  
  

You can read more information about pipelines in the [documentation](<https://www.graphicsmill.com/docs/gm/processing-large-images-using-pipelines.htm>).

## Instruction

Use the Pipeline API to resize an input image and overlay a watermark image,
configuring element properties inside OnInit event handlers instead of hard-coding
dimensions up front.

## Usings

```csharp
using Aurigma.GraphicsMill.Codecs;
using Aurigma.GraphicsMill.Transforms;
```

## Code Snippet

```csharp
// Create pipeline elements
using (var reader = ImageReader.Create("Venice.jpg"))
using (var resize = new Resize())
using (var writer = ImageWriter.Create("OnInitWatermark.jpg"))
{
    // Configure the resize element based on the input image size
    resize.OnInit += (sender, e) =>
    {
        var resizeSender = sender as Resize;
        resizeSender.Width = (int)(e.ImageParams.Width / 2.5f);
        resizeSender.Height = (int)(e.ImageParams.Height / 2.5f);
        resizeSender.ResizeMode = ResizeMode.Resize;
        resizeSender.InterpolationMode = ResizeInterpolationMode.Anisotropic9;
    };

    // Create pipeline elements for the watermark
    using (var topReader = ImageReader.Create("Stamp.png"))
    using (var topResize = new Resize(200, 200, ResizeInterpolationMode.High))
    using (var combiner = new Combiner(CombineMode.Alpha, (topReader + topResize).Build()))
    {
        // Configure the topResize element based on the resized base image size
        topResize.OnInit += (sender, e) =>
        {
            var topResizeSender = sender as Resize;
            topResizeSender.ResizeMode = ResizeMode.Fit;
            topResizeSender.Width = resize.Width / 2;
            topResizeSender.Height = resize.Height / 2;

            resize.InterpolationMode = ResizeInterpolationMode.Anisotropic9;
        };

        // Configure the combiner element to center the watermark
        combiner.OnInit += (sender, e) =>
        {
            var args = e as CombinerOnInitEventArgs;
            var combinerSender = sender as Combiner;
            combinerSender.X = (args.ImageParams.Width - args.TopImageParams.Width) / 2;
            combinerSender.Y = (args.ImageParams.Height - args.TopImageParams.Height) / 2;
        };

        // Run the pipeline
        Pipeline.Run(reader + resize + combiner + writer);
    }
}
```

## Explanation

The code:

- Reads the base image "Venice.jpg" with ` ImageReader `.
- Uses a ` Resize ` element whose ` Width `, ` Height `, ` ResizeMode `, and
` InterpolationMode ` are set in its ` OnInit ` handler based on the actual input
image size (scaled by a factor of 2.5 while preserving aspect ratio).
- Reads the watermark image "Stamp.png" with another ` ImageReader `.
- Resizes the watermark in a second ` Resize ` element, configuring its ` ResizeMode `
and target size in ` OnInit ` so the watermark is half the width and height of the
resized base image.
- Combines the base image and watermark with a ` Combiner ` element, setting its ` X `
and ` Y ` in the ` OnInit ` handler so the watermark is centered.
- Runs the pipeline ` reader + resize + combiner + writer ` with ` Pipeline.Run `.
- Saves the result as "OnInitWatermark.jpg".

## Tags

- [Advanced](<https://www.graphicsmill.com/samples/tags/advanced_md>): Demonstrating advanced usage scenarios
- [Branched Pipeline](<https://www.graphicsmill.com/samples/tags/branched-pipeline_md>): Building branched processing pipelines
- [Resize](<https://www.graphicsmill.com/samples/tags/resize_md>): Resizing images

## Input

- [Venice.jpg](<https://www.graphicsmill.com/samples/LargeImages/compiled/Debug/Venice_jpg>) (preview)
- [Stamp.png](<https://www.graphicsmill.com/samples/LargeImages/compiled/Debug/Stamp_png>) (preview)

## Output

- [OnInitWatermark.jpg](<https://www.graphicsmill.com/samples/LargeImages/compiled/Debug/OnInitWatermark_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.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.OnInitEventArgs.ImageParams](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_OnInitEventArgs_ImageParams.htm.md>)
- [Aurigma.GraphicsMill.Transforms.CombineMode](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Transforms_CombineMode.htm.md>)
- [Aurigma.GraphicsMill.Transforms.Combiner](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Transforms_Combiner.htm.md>)
- [Aurigma.GraphicsMill.Transforms.Combiner.X](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Transforms_Combiner_X.htm.md>)
- [Aurigma.GraphicsMill.Transforms.Combiner.Y](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Transforms_Combiner_Y.htm.md>)
- [Aurigma.GraphicsMill.Transforms.CombinerOnInitEventArgs](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Transforms_CombinerOnInitEventArgs.htm.md>)
- [Aurigma.GraphicsMill.Transforms.CombinerOnInitEventArgs.TopImageParams](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Transforms_CombinerOnInitEventArgs_TopImageParams.htm.md>)
- [Aurigma.GraphicsMill.Transforms.Resize](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Transforms_Resize.htm.md>)
- [Aurigma.GraphicsMill.Transforms.Resize.InterpolationMode](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Transforms_Resize_InterpolationMode.htm.md>)
- [Aurigma.GraphicsMill.Transforms.Resize.ResizeMode](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Transforms_Resize_ResizeMode.htm.md>)
- [Aurigma.GraphicsMill.Transforms.ResizeInterpolationMode](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Transforms_ResizeInterpolationMode.htm.md>)
- [Aurigma.GraphicsMill.Transforms.ResizeMode](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Transforms_ResizeMode.htm.md>)
