Two + plugins used
1. File Upload plugin uploadify
2. Image editing plugin jquery. Jcrop
Based on the MVC design pattern of ASP, the plug-in is designed.
Paste the following code:
View (. cshtml):
Controller:
Public ActionResult uploadportrait () {return View (); } [HttpPost] public actionresult upload (httppostedfilebase Filedata) {try { Image image = Image.fromstream (Filedata.inputstream); String ipath = Path.Combine ("Images", Path.getfilename (Filedata.filename)); String spath = Path.Combine (System.AppDomain.CurrentDomain.BaseDirectory, Ipath); Image. Save (spath); Return Json (new {id = "0", mess = string. Format ("{0}{1}/{2}", BaseUrl, "Images", filedata.filename), IW = image. Width, ih = image. Height}); } catch (Exception ex) {return Json (new {id = "1", mess = ex. Message}); }} public ActionResult Trycrop (string img, int x, int y, int w, int h) {var imgpath = im G.split ('/'); Image image = Image.FromFile (Path.Combine (HttpContext.Server.MapPatH ("~"), "Images", Imgpath[imgpath.count ()-1]); Crop (Image, W, h, x, y). Save (Path.Combine (HttpContext.Server.MapPath ("~"), "Res", User.Identity.Name + "portrait.jpg")); Return redirecttoaction ("Example", "account"); } public string BaseUrl {get {return Request.Url.Scheme + "://" + Req Uest. Url.authority + Request.ApplicationPath.TrimEnd ('/') + '/'; }} [Nonaction] public static image Crop (image image, int width, int height, int x, int y) { Bitmap bmp = New Bitmap (, PIXELFORMAT.FORMAT24BPPRGB); Bmp. Setresolution (image. HorizontalResolution, image. VerticalResolution); using (Graphics graphic = graphics.fromimage (BMP)) {graphic. SmoothingMode = Smoothingmode.antialias; Graphic. Interpolationmode = Interpolationmode.highqualitybicubic; Graphic. Pixeloffsetmode = PixelOffsetMode.Highquality; Graphic. DrawImage (Image, new Rectangle (0, 0, N, N), x, y, width, height, graphicsunit.pixel); } return BMP; }
Design method for Avatar upload plugin compatible with IE (ASP. NET MVC)