asp.net picture cutting

Source: Internet
Author: User
Picture path
String OldPath = Server.MapPath ("/111.jpg");
New picture Path
String NewPath = System.IO.Path.GetExtension (OldPath);
Set the coordinates and size of the interception
int x =, y = 0, Width =, height = 500;


Calculates the new file name and adds _new to 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>");
Defining a truncated rectangle
System.Drawing.Rectangle Croparea = new System.Drawing.Rectangle (x, y,width, height); The size of the area to intercept


Load picture
System.Drawing.Image img = System.Drawing.Image.FromStream (New System.IO.MemoryStream System.IO.File.ReadAllBytes ( OldPath)));


Determine where the excess is
If (img. Width < x + width) | | Img. Height < y + height)
{
Response.Write ("The intercepted area exceeds the height and width of the picture itself");
Img. Dispose ();
Return
}
Defining Bitmap objects
System.Drawing.Bitmap bmpimage = new System.Drawing.Bitmap (IMG);


to crop
System.Drawing.Bitmap Bmpcrop = Bmpimage.clone (Croparea, Bmpimage.pixelformat);


Save as a new file
Bmpcrop.save (NewPath);


Releasing objects
Img. Dispose ();
Bmpcrop.dispose ();
Response.Write ("cutting success");

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.