Home >Backend Development >C++ >How to Crop an Image in C#?

How to Crop an Image in C#?

Barbara Streisand
Barbara StreisandOriginal
2025-01-27 20:18:11757browse

How to Crop an Image in C#?

C# image cropping method

When working with images in C#, you may need to crop the image to focus on a specific area. How to crop an image using C#?

The following code demonstrates how to crop an image in C#:

<code class="language-csharp">private static Image cropImage(Image img, Rectangle cropArea)
{
   Bitmap bmpImage = new Bitmap(img);
   return bmpImage.Clone(cropArea, bmpImage.PixelFormat);
}</code>

Example:

<code class="language-csharp">Image originalImage = Image.FromFile("original.jpg");
Rectangle cropArea = new Rectangle(100, 100, 200, 200);
Image croppedImage = cropImage(originalImage, cropArea);
croppedImage.Save("cropped.jpg");</code>

This code crops the original image into a rectangle of size 200x200, starting at (100, 100) pixels. The cropped image will be saved as "cropped.jpg".

More resources:

The above is the detailed content of How to Crop an Image in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn