Transforming Images with Pipelines

This article helps you to understand how to use the major Graphics Mill transforms with the pipeline syntax. We will not learn all the aspects of the transforms here and we presume that you are already familiar with them. If not, please review how to use the transforms with the regular syntax first. Here are the sections we are going to cover with this article:

Resizing and Cropping

Resizing

The main parameters for the resize transform are width and height of resulting images. If you want Graphics Mill to preserve the aspect ratio of the image, you should specify only one dimension. Resize quality and speed depends on specified interpolation mode. Better quality means lower speed. The ResizeInterpolationMode enumeration exposes all available interpolation modes.

The following code proportionally resizes images to 2048 pixel width:

C#
using (var reader = new JpegReader(@"Images\in.jpg"))
using (var resize = new Resize(2048, 0, ResizeInterpolationMode.High))
using (var writer = new JpegWriter(@"Images\Output\out.jpg"))
{
    Pipeline.Run(reader + resize + writer);
}

Cropping

Cropping requires you to pass a crop rectangle as an input parameter. The rectangle can be set as four System.Int32 values: x and y coordinates of the upper left rectangle corner, rectangle width, and rectangle height. Or you can set the rectangle as a Rectangle instance.

The following code crops 50x100 rectangle out of the image and saves the result in JPEG format:

C#
using (var reader = new JpegReader(@"Images\in.jpg"))
using (var crop = new Crop(50, 60, 50, 100))
using (var writer = new JpegWriter(@"Images\Output\out.jpg"))
{
    Pipeline.Run(reader + crop + writer);
}

Rotating and Flipping

Rotating

In Graphics Mill positive degree numbers cause clockwise rotation and negative degree numbers cause counter-clockwise rotation. A special cases are rotation by 90, 180, or 270 degrees, because it uses a special algorithm which avoids floating-point calculations yielding to higher performance. In these cases you get the rotated version of the source image without additional background. In other cases the rotated image will have margins, filled with background color, making it rectangular.

If you do not specify a background color and an interpolation mode, the transparent white color and the high quality interpolation mode are used. Rotation quality and speed depend on the specified interpolation mode. Better quality means lower speed. The InterpolationMode enumeration exposes the available interpolation modes.

You can specify a background color and an interpolation mode as the constructor arguments or through the Rotate.BackgroundColor and Rotate.Rotate.InterpolationMode properties.

The following code rotates an image by 5 degrees clockwise using the Bitmap.Transforms property:

C#
using (var reader = new JpegReader(@"Images\in.jpg"))
using (var rotate = new Rotate(5, RgbColor.Yellow, InterpolationMode.High))
using (var writer = new JpegWriter(@"Images\Output\out.jpg"))
{
    Pipeline.Run(reader + rotate + writer);
}

Flipping

To flip an image you have to specify one of the following flip types: Horizontal, Vertical, or Both.

The following code flips an image horizontally:

C#
using (var reader = new JpegReader(@"Images\in.jpg"))
using (var flip = new Flip(FlipType.Horizontal))
using (var writer = new JpegWriter(@"Images\Output\out.jpg"))
{
    Pipeline.Run(reader + flip + writer);
}

Overlaying Images

Graphics Mill provides features for combining images, including watermarking and masked overlaying (collages). You may select one of many available combine modes and set the foreground image coordinates.

To watermark an image you should configure the Combiner as follows:

  1. Set the Combiner.Mode property to Alpha.
  2. Prepare an overlay image and pass it to the Combiner.TopImage property. This property allows you not to load the whole image to memory as it accepts Pipeline. In our sample we read an image from a file and make it semitransparent.
  3. Position an overlay image via Combiner.X and Combiner.Y properties.
C#
using (var reader = new JpegReader(@"Images\in.jpg"))
using (var watermark = new PngReader(@"Images\watermark.png"))
using (var combiner = new Combiner())
using (var writer = new JpegWriter(@"Images\Output\out.jpg"))
{
    var overlay = new Pipeline();
    overlay.Add(watermark);
    overlay.Add(new ScaleAlpha(0.8f));
    
    combiner.Mode = CombineMode.Alpha;
    combiner.TopImage = overlay;
    combiner.X = 10;
    combiner.Y = reader.Height - watermark.Height - 40;
    combiner.AutoDisposeTopImage = true;

    Pipeline.Run(reader + combiner + writer);
}

Converting Colors

Converting pixels between different color spaces, you can choose whether to apply color management. If you do not use color management, it works faster, but conversion is inaccurate. Colors may look non-realistic. On the other hand, color management allows preserving natural colors, but it slower. To choose the color management engine, use the ColorConverter.ColorManagementEngine property. When it is LittleCms or AdobeCmm, color management is enabled, if None - disabled. Also, color management can be successfully used only if input and output profiles are provided.

To convert image colors from RGB to CMYK you need to specify the color profile of the CMYK color space (which corresponds to the device where we want to display this image, for example your printer). The following code demonstrates color conversion with Euroscale Coated profile.

C#
using (var reader = new JpegReader(@"Images\in.jpg"))
using (var converter = new ColorConverter(PixelFormat.Format32bppCmyk))
using (var writer = new JpegWriter(@"Images\Output\out.jpg"))
{
    converter.ColorManagementEngine = ColorManagementEngine.LittleCms;
    converter.DestinationProfile = new ColorProfile(
        @"C:\windows\system32\spool\drivers\color\EuroscaleCoated.icc");
    Pipeline.Run(reader + converter + writer);
}

See Also

Reference

Manual