Disposing Pipeline Elements

All the pipeline elements implement the IDisposable interface, so you can release their resources by calling Dispose(). Although pipeline elements are disposed during garbage collection, the problem is that Garbage Collector might not run for some time. So to make sure that an element's resources are released once you're finished with it, you should manually call the Dispose() method or instantiate the element in the using statement.

C#
using (var reader = new Aurigma.GraphicsMill.Codecs.JpegReader(@"Images\in.jpg"))
using (var resizer = new Aurigma.GraphicsMill.Transforms.Resize(2048, 0))
using (var writer = new Aurigma.GraphicsMill.Codecs.JpegWriter(@"Images\Output\out.jpg"))
{
    resizer.InterpolationMode = Aurigma.GraphicsMill.Transforms.ResizeInterpolationMode.High;
    writer.Exif = reader.Exif;

    Aurigma.GraphicsMill.Pipeline.Run(reader + resizer + writer);
}

In addition to common disposing facilities, Graphics Mill provides two more approaches to dispose of pipeline elements.

Disposing All Elements

You can dispose all elements of a pipeline at once by calling the Pipeline.DisposeAllElements() method. This approach allows you to avoid creating local variables pointing to pipeline elements in order to be able to call the Dispose() method for each of them.

Here is the code to demonstrate this approach:

C#
var pipeline = new Aurigma.GraphicsMill.Pipeline()
{
    Aurigma.GraphicsMill.Codecs.ImageReader.Create(@"Images\in.jpg"),
    new Aurigma.GraphicsMill.Transforms.Resize(2048, 0),
    Aurigma.GraphicsMill.Codecs.ImageWriter.Create(@"Images\Output\out.jpg")
};

pipeline.Run();
pipeline.DisposeAllElements();

Disposing Inner Elements

Some pipeline elements (i.e. ChannelCombiner) implement properties holding other pipelines. In order to dispose of these inner pipelines, such elements provide AutoDisposeXXX properties specifying whether the inner pipeline should be disposed along with the element.

All the pipeline elements supporting auto-disposing of inner pipelines are listed below.

C#
using (var alpha = new Aurigma.GraphicsMill.Transforms.SetAlpha())
{
    alpha.AlphaSource = Aurigma.GraphicsMill.Codecs.ImageReader.Create(@"Images\alpha.png");
    alpha.AutoDisposeAlphaSource = true;

    Aurigma.GraphicsMill.Pipeline.Run(@"Images\in.jpg" + alpha + @"Images\Output\out.png");
}

See Also

Reference

MSDN Resources