The complete code is as follows:
C # code
// Image path
String oldPath = Server. MapPath ("~ /Aaaa.jpg ");
// New image path
String newPath = System. IO. Path. GetExtension (oldPath );
// Set the coordinate and size of the screenshot.
Int x = 0, y = 10, width = 200, height = 200;
// Calculate the new file name and add _ new after the old file name
NewPath = oldPath. Substring (0, oldPath. Length-newPath. Length) + "_ new" + newPath;
Response. Write (oldPath );
Response. Write ("<br> ");
Response. Write (newPath );
Response. Write ("<br> ");
// Define the truncated rectangle
System. Drawing. Rectangle cropArea = new System. Drawing. Rectangle (x, y, width, height); // area size to be truncated
// Load the image
System. Drawing. Image img = System. Drawing. Image. FromStream (new System. IO. MemoryStream (System. IO. File. ReadAllBytes (oldPath )));
// Determine whether the location exceeds the limit
If (img. Width <x + width) | img. Height <y + height)
{
Response. Write ("the intercepted area exceeds the height and width of the image .");
Img. Dispose ();
Return;
}
// Define a Bitmap object
System. Drawing. Bitmap BMP image = new System. Drawing. Bitmap (img );
// Crop www.2cto.com
System. Drawing. Bitmap bmpcdrop = BMP image. Clone (cropArea, BMP image. PixelFormat );
// Save as a new file
BmpCrop. Save (newPath );
// Release the object
Img. Dispose ();
BmpCrop. Dispose ();
Author: Meng xianhui