Image Clipping in C#

Image clipping, also known as image masking or object extraction, is a technique used in graphic design and image processing. It is used to isolate specific objects or subjects from an image. In this article, we will explore image clipping implementation in C#. Learn step-by-step techniques to extract objects and enhance your designs.

The following topics shall be covered in this article:

  1. C# Image Clipping API - Free Download
  2. How to Implement C# Image Clipping
  3. Extract Object from Image in C#
  4. Free Learning Resources

C# Image Clipping API - Free Download

For implementing image clipping in C#, we will be using the Aspose.Drawing for .NET API. It is a cross-platform 2D graphics library for drawing text, geometries, and images programmatically. It allows loading, saving, and manipulating the supported file formats.

Please either download the DLL of the API or install it using NuGet.

PM> Install-Package Aspose.Drawing

C# Image Clipping

We can implement image clipping programmatically by following the steps given below:

  1. Firstly, create an instance of the Bitmap class.
  2. Next, create the Graphics class object using the FromImage() method.
  3. Then, define the clip path using the GraphicsPath class.
  4. After that, set the clip path using the SetClip() method.
  5. Finally, save the output image using the Save() method.

The following code sample shows how to implement image clipping in C#.

// Create a new bitmap
var bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
// Initialize Graphics
Graphics graphics = Graphics.FromImage(bitmap);
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
// Define Clip
Rectangle rectangle = new Rectangle(200, 200, 600, 400);
GraphicsPath clipPath = new GraphicsPath();
clipPath.AddEllipse(rectangle);
graphics.SetClip(clipPath);
// Specify string
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
Brush brush = new SolidBrush(Color.Red);
Font arial = new Font("Arial", 20, FontStyle.Regular);
string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
"Maecenas sapien tellus, mattis et condimentum eget, commodo ut ipsum. " +
"Maecenas elit sapien, tempus sit amet mauris sit amet, hendrerit laoreet nisi. " +
"Nulla facilisi. Sed commodo, mauris eget porta commodo, nunc tellus volutpat mi, " +
"eu auctor diam libero vel neque. Vestibulum nec mattis dui, nec molestie nisl. " +
"Etiam in magna felis. Praesent non nulla tortor. Integer nec convallis purus. " +
"Fusce vitae mollis mauris. Cras efficitur dui at mi viverra scelerisque. " +
"Morbi quis magna elit. Nulla facilisis id ante sit amet fringilla. " +
"Sed iaculis consectetur lectus a interdum. Etiam ut sollicitudin lectus, et congue lectus.";
// Draw string
graphics.DrawString(text, arial, brush, rectangle, stringFormat);
// Save
bitmap.Save("C:\\Files\\Clipping.png");
C# Image Clipping

C# Image Clipping

Extract Object from Image in C#

We can extract a specific part of an image using image clipping by following the steps mentioned earlier. However, we just need to load an existing image and draw it using the DrawImage() method.

The following code sample shows how to use extract object from an image in C#.

// Create a new bitmap
var bitmap = new Bitmap(225, 225, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
// Initialize Graphics
Graphics graphics = Graphics.FromImage(bitmap);
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
// Define Clip
Rectangle rectangle = new Rectangle(0, 0, 225, 225);
GraphicsPath clipPath = new GraphicsPath();
clipPath.AddEllipse(rectangle);
graphics.SetClip(clipPath);
// Load the image
Bitmap image = new Bitmap(@"C:\Files\aspose-logo.png");
// Draw image at specified location
graphics.DrawImage(image,0,0);
bitmap.Save("C:\\Files\\extract-object.png");
Extract Object from Image in C#

Extract Object from Image in C#

Get Free Temporary License

You can get a free temporary license to try Aspose.Drawing for .NET without evaluation limitations.

Aspose.Drawing – Free Learning Resources

Besides image clipping in C#, you can learn more about various features of the library using the resources given below:

Conclusion

In this article, we learned how to implement an image clipping in C#. By following the step-by-step instructions and code examples, you can easily extract objects from images with precision. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also