Home > Backend Development > C++ > How to Crop Images in C# Using Image.Crop() and Bitmap.Clone()?

How to Crop Images in C# Using Image.Crop() and Bitmap.Clone()?

Mary-Kate Olsen
Release: 2025-01-27 20:05:08
Original
1006 people have browsed it

How to Crop Images in C# Using Image.Crop() and Bitmap.Clone()?

C# image cropping method

Picture cropping is a common task in image processing, used to remove unwanted parts of a picture, highlight specific areas, or improve the composition of the picture. This article introduces two efficient C# image cropping methods:

Use Image.Crop() method:

Question: How to crop an image using the Image.Crop() method in C#?

Answer:

The Image.Crop() method needs to specify the rectangular area to be cropped. An example is as follows:

Image img = Image.FromFile("image.jpg");
Rectangle cropArea = new Rectangle(100, 100, 200, 200);
Image croppedImage = img.Clone() as Image;
croppedImage.Crop(cropArea);
croppedImage.Save("cropped-image.jpg");
Copy after login

Use Bitmap.Clone() method:

Question: Can I use Bitmap.Clone() method instead of Image.Crop() method to crop an image?

Answer:

Yes. The Bitmap.Clone() method creates a new image based on the specified rectangle. This method is slightly faster than the Image.Crop() method when working with bitmaps:

Bitmap bmpImage = new Bitmap(img);
Rectangle cropArea = new Rectangle(100, 100, 200, 200);
Image croppedImage = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
croppedImage.Save("cropped-image.jpg");
Copy after login

The above is the detailed content of How to Crop Images in C# Using Image.Crop() and Bitmap.Clone()?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template