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");
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");
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!