---
title: "Disposing Pipeline Elements"
canonical: https://www.graphicsmill.com/docs/gm/disposing-pipeline-elements.htm
product: "Graphics Mill"
---

# Disposing Pipeline Elements
 
All the pipeline elements implement the [IDisposable](https://learn.microsoft.com/en-us/dotnet/api/system.idisposable?view=netframework-4.8) interface, so you can release their resources by calling [Dispose()](https://learn.microsoft.com/en-us/dotnet/api/system.idisposable.dispose?view=netframework-4.8). 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.
 
```csharp
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()](https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Pipeline_DisposeAllElements.htm.md) 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:
 
```csharp
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](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_ChannelCombiner.htm.md)) implement properties holding other pipelines. In order to dispose of these inner pipelines, such elements provide **AutoDispose*XXX***  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.
 
- [ChannelCombiner.AutoDisposeSources](https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_ChannelCombiner_AutoDisposeSources.htm.md) is demonstrated in the [Reading and Writing Channels](https://www.graphicsmill.com/docs/gm/reading-and-writing-channels.htm.md) topic.
- [Combiner.AutoDisposeTopImage](https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Transforms_Combiner_AutoDisposeTopImage.htm.md) is demonstrated in the [Transforming Images with Pipelines](https://www.graphicsmill.com/docs/gm/transforming-images-with-pipelines.htm.md) topic.
- [SetAlpha.AutoDisposeAlphaSource](https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Transforms_SetAlpha_AutoDisposeAlphaSource.htm.md) is demonstrated in the following code snippet.

```csharp
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
 
- [PipelineElement.Dispose() Method](https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_PipelineElement_Dispose.htm.md)
- [Pipeline.DisposeAllElements() Method](https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Pipeline_DisposeAllElements.htm.md)

### MSDN Resources
 
- [using Statement (C# Reference)↗](http://msdn.microsoft.com/en-us/library/yh598w02.aspx)
