Protected void Page_Load (object sender, EventArgs e) { If (IsPostBack) { Int x1 = Convert. ToInt32 (Request ["x1"]); Int y1 = Convert. ToInt32 (Request ["y1"]); Int x2 = Convert. ToInt32 (Request ["x2"]); Int y2 = Convert. ToInt32 (Request ["y2"]);
HttpFileCollection files = Request. Files; For (int I = 0; I <files. Count; I ++) { HttpPostedFile file = files [I]; File. SaveAs (Server. MapPath ("Upload/" + file. FileName ));
// Set the thumbnail Int Thumbnailwidth = 400; Int Thumbnailheight = 300; // Create a bmp Image Bitmap bitmap = new Bitmap (Thumbnailwidth, Thumbnailheight ); // Create a canvas Graphics graphic = Graphics. FromImage (bitmap ); // Set a high quality Interpolation Method Graphic. InterpolationMode = InterpolationMode. High; // Set high quality and smooth Low Speed Graphic. SmoothingMode = SmoothingMode. HighQuality; // Clear the canvas and fill it with a transparent background color Graphic. Clear (System. Drawing. Color. Transparent ); // Original Image Bitmap originalImage = new Bitmap (file. InputStream ); // Draw the specified part of the original image at the specified position and in the specified size Graphic. DrawImage (originalImage, new System. Drawing. Rectangle (0, 0, Thumbnailwidth, Thumbnailheight ), New System. Drawing. Rectangle (0, 0, originalImage. Width, originalImage. Height ), System. Drawing. GraphicsUnit. Pixel ); // Obtain the thumbnail System. Drawing. Image ThumbnailImage = System. Drawing. Image. FromHbitmap (bitmap. GetHbitmap ()); // Create a selected Image Bitmap selectbitmap = new Bitmap (x2-x1, y2-y1 ); // Create a canvas Graphics selectgraphic = Graphics. FromImage (selectbitmap );
// Cropping Selectgraphic. DrawImage (ThumbnailImage, 0, 0, new Rectangle (x1, y1, x2-x1, y2-y1), GraphicsUnit. Pixel ); // Save Selectbitmap. Save (Server. MapPath ("Upload/" + Guid. NewGuid () + file. FileName), ImageFormat. Jpeg ); // Todo: Release the above resources } } } |