Remove Background from Image using C#

This tutorial describes how to remove background from image using C#. It has IDE settings, a list of steps, and a sample code for a photo background remover using C#. Finally, it saves the processed image with a transparent background.

Steps to Delete Background from Image using C#

  1. Set the IDE to use Aspose.Imaging for .NET to remove the picture background
  2. Define the working directory and file paths for the input JPEG image and the output PNG results
  3. Load the input image as a raster image to enable pixel-based processing
  4. Configure the GraphCut masking options, including automatic stroke detection, feathering for smooth edges, transparency settings, and output file format
  5. Initialize the masking engine and execute the background removal operation using the configured GraphCut options with the Decompose() method
  6. Extract the segmented foreground result from the returned masking output
  7. Save the final processed image as a transparent PNG file and automatically release resources after processing

These steps define the process for developing a background eraser using C#. The program loads an image and uses Aspose.Imaging’s GraphCut-based masking to automatically detect and remove the background with smooth edge feathering and transparent replacement. It then extracts the foreground result and saves it as a high-quality PNG file with transparency, using configured export options and proper resource handling.

Code for Background Remover using C#

The above code demonstrates the process of developing a picture background remover using C#. The parameter CalculateDefaultStrokes enables the automatic detection of the foreground and background, FeatheringRadius smooths the edge calculated based on image size, and the Method uses the GraphCut algorithm for segmentation. Setting the property Decompose to false removes only the background and doesn’t break into multiple layers.

This article has taught us the process of removing background from images. To change image brightness, refer to the article on Change Image Brightness using C#.

 English