This article describes the method of cropping websites to upload pictures based on .net. Since the client Javascript cannot operate files, you can only upload the image first and then cut it on the server side.
1. Upload pictures
2. Javascript cuts the picture (actually just selects the part to be cut)
3. Server-side cutting
(1) Cut in the page's cs file. Several hidden controls must be placed to return the coordinates selected by js.
The source code of the cut picture is as follows:
using System; using ; using ; using ; public class Cut { /// <summary> /// Crop the picture /// </summary> /// <param name="sourceImg">original picture path</param> /// <param name="desImg">Crop the image path</param> /// <param name="left">X</param> /// <param name="top">Y</param> /// <param name="width">Width</param> /// <param name="height">high</param> public static void CutImage(string sourceImg, string desImg, int left, int top, int width, int height) { img = (sourceImg); imgToSave = new (width, height); g = (imgToSave); RectangleF sourceRect = new RectangleF(left, top, width, height); RectangleF destinationRect = new RectangleF(0, 0, width, height); (img, destinationRect, sourceRect, ); (); (desImg, ); (); (); (); } }
(2) Cut in ashx to return the file stream. Pass coordinates with parameters.
using System; using ; using ; using ; public class ImgCropper_WebHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { string Pic = (["p"]); int PointX = Convert.ToInt32(["x"]); int PointY = Convert.ToInt32(["y"]); int CutWidth = Convert.ToInt32(["w"]); int CutHeight = Convert.ToInt32(["h"]); int PicWidth = Convert.ToInt32(["pw"]); int PicHeight = Convert.ToInt32(["ph"]); = "image/jpeg"; ResetImg(context, (Pic), PicWidth, PicHeight, PointX, PointY, CutWidth, CutHeight).WriteTo(); } public MemoryStream ResetImg(HttpContext context, string ImgFile, int PicWidth, int PicHeight, int PointX, int PointY, int CutWidth, int CutHeight) { Image imgPhoto = (ImgFile); Bitmap bmPhoto = new Bitmap(CutWidth, CutHeight, .Format24bppRgb); Graphics gbmPhoto = (bmPhoto); (imgPhoto, new Rectangle(0, 0, CutWidth, CutHeight), PointX * / PicWidth, PointY * / PicHeight, CutWidth * / PicWidth, CutHeight * / PicHeight, ); //Save the picture to the server (("upload/") + () + ".jpg", ); //Create file streaming back MemoryStream ms2 = new MemoryStream(); (ms2, ); (); (); (); return ms2; } public bool IsReusable { get { return false; } } }