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

How to Crop an Image in C#?

Barbara Streisand
Release: 2025-01-27 20:18:11
Original
846 people have browsed it

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#:

private static Image cropImage(Image img, Rectangle cropArea)
{
   Bitmap bmpImage = new Bitmap(img);
   return bmpImage.Clone(cropArea, bmpImage.PixelFormat);
}
Copy after login

Example:

Image originalImage = Image.FromFile("original.jpg");
Rectangle cropArea = new Rectangle(100, 100, 200, 200);
Image croppedImage = cropImage(originalImage, cropArea);
croppedImage.Save("cropped.jpg");
Copy after login

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 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