Remove Image Background C#

In digital content creation and manipulation, removing the background from images is a common and essential task. Whether you’re working on graphic design, e-commerce, or any other visual project, having the ability to isolate objects from their backgrounds widens your creativity. In this blog post, we’ll explore how to remove background from images programmatically in C#.

C# Library to Remove Background from Images

To remove background from images, we will use Aspose.Imaging for .NET - a powerful library that provides a wide range of image processing features for .NET applications. It supports various image formats and allows developers to perform operations like resizing, cropping, rotating, and, of course, removing backgrounds with ease.

You can either download the API or install it from NuGet.

PM> Install-Package Aspose.Imaging

Removing Image Background with Auto Masking in C#

To receive better background removing results, auto masking method is preferred. The goal is to create a mask that accurately identifies the pixels belonging to the foreground and those belonging to the background. Aspose.Imaging provides three auto masking techniques to remove background from image, as described in the following sections.

Graph Cut Auto Masking with Feathering

In this technique, the API performs image masking with feathering based on image size and uses auto calculated default strokes. The following code snippet shows how to use graph cut auto masking with feathering. The Args property of AutoMaskingGraphCutOptions can be omitted since default strokes are placed there in the end.

The following screenshot shows the input and output images.

removing image background in C#

Re-using Default Strokes in Repeated Auto Masking

In this technique, the image masking is done using auto calculated default strokes, however, the API re-uses the masking options for the new masking iteration. In the following code snippet, you can notice that the data of the two assumed objects is also specified in the AssumedObjects property of the AutoMaskingGraphCutOptions. Following the initial masking results, adjustments are made to the applied background/foreground strokes, and a subsequent iteration of masking is carried out.

The following screenshot shows the input and output images.

Repeated auto masking for background removal

Graph Cut Auto Masking with Specified Assumed Object Data

In this technique, the data of a specific assumed object is used in the AssumedObjects property of the AutoMaskingGraphCutOptions, which is used in subsequent masking iterations. The following code snippet shows how to use graph cut auto masking with one assumed object to remove background of an image.

The following screenshot shows the input and output images.

Graph Cut Auto Masking with Assumed Object

Image Background Removal using Manual Masking

If you are not interested in auto masking, you can use manual masking method to remove background from images. The following code snippet shows how to apply manual masking to a raster image to remove its background.

The following screenshot shows the input and output images.

Manual Masking for Background Removal

Get C# Image Background Remover API

You can get a free temporary license and use our image editing and background removing library without any limitations.

Conclusion

Removing backgrounds from images is a crucial skill in image editing and design. Aspose.Imaging for .NET lets developers precisely remove backgrounds from images in C# applications. This blog post covered the basics of loading images, implementing background removal using different methods of auto and manual masking, and saving the output image.

By integrating Aspose.Imaging into your C# projects, you can streamline the image processing workflow and unlock a new level of creativity in your applications. Experiment with different features and techniques to discover the full potential of Aspose.Imaging for .NET. Also, share your queries with us via our forum.

See Also