crop image in csharp

You can crop an image in C# using System.Drawing.Bitmap class. You need to specify the region of the image that you want to crop and create a new Bitmap object with the specified region.

Here's the code to crop an image in C#:

main.cs
// Load the image file
Bitmap originalImage = new Bitmap(@"C:\path\to\image.jpg");

// Set the region of the image that you want to crop
Rectangle cropRegion = new Rectangle(10, 10, 100, 100);

// Create a new bitmap object with the specified region
Bitmap croppedImage = originalImage.Clone(cropRegion, originalImage.PixelFormat);

// Save the cropped image to a file
croppedImage.Save(@"C:\path\to\cropped-image.jpg");
421 chars
12 lines

In the above code, we load the original image using Bitmap class and set the region of the image that we want to crop using Rectangle class. Then we create a new Bitmap object with the specified region using Clone method of Bitmap class. Finally, we save the cropped image to a file using Save method of Bitmap class.

gistlibby LogSnag