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 JpegReader(@"Images\in.jpg"))
using (var resizer = new Resize(2048, 0))
using (var writer = new JpegWriter(@"Images\Output\out.jpg"))
{
    resizer.InterpolationMode = ResizeInterpolationMode.High;
    writer.Exif = reader.Exif;

    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 Pipeline()
{
    ImageReader.Create(@"Images\in.jpg"),
    new Resize(2048, 0),
    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 SetAlpha())
{
    alpha.AlphaSource = ImageReader.Create(@"Images\alpha.png");
    alpha.AutoDisposeAlphaSource = true;

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

See Also

Reference

MSDN Resources