Code for cropping Images

Source: Internet
Author: User
Tags bmp image

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.