public JsonResult CropImage(ChangeAvatar model) { var imageName = model.ImageLink.Substring(model.ImageLink.LastIndexOf('/') + 1, model.ImageLink.Length - 1 - model.ImageLink.LastIndexOf('/')); var bitmap = new Bitmap(Server.MapPath("~/Tmp/" + imageName)); double ratio = (float)bitmap.Width / 400; var cropRect = new Rectangle((int)Math.Round(model.X * ratio), (int)Math.Round(model.Y * ratio), (int)Math.Round(model.Width * ratio), (int)Math.Round(model.Height * ratio)); var mem = new MemoryStream(); using (var img = new ImageFactory()) { img.Load(Server.MapPath("~/Tmp/" + imageName)); img.Crop(cropRect).Save(mem); } bitmap.Dispose(); //Delete tmp file System.IO.File.Delete(Server.MapPath("~/Tmp/" + imageName)); imageName = imageName.Substring(0, imageName.LastIndexOf('.')) + ".jpg"; var member = MemberRepository.ChangeAvatar(SessionManager.UserLogged.UserId, imageName, Convert.ToBase64String(mem.ToArray()), null); if (member.Code == "1") { SessionManager.UserLogged.AvatarMedium = member.Message; SessionManager.UserLogged.Avatar = member.Message.Replace("medium_", ""); } return member.Code == "-1" ? Json(new {Result = false, ErrorMessage = member.Message}) : Json(new { Result = true, Link = member.Message }); }
public ActionResult ChangeAvatar(ChangeAvatar model) { try { if (model.FileImage != null && model.FileImage.ContentLength > 0 && !model.FileImage.ContentType.Contains("gif")) { model.FileImage.SaveAs(Server.MapPath("~/Tmp/" + model.FileImage.FileName)); model.ImageLink = "/Tmp/" + model.FileImage.FileName; model.Type = false; SessionManager.GlobalAvatar = model; return View(model); } return View(model); } catch (Exception exception) { Logger.Error(exception.Message + exception.InnerException.Message); return View(new ChangeAvatar(){ImageLink = exception.Message + exception.InnerException.Message}); } }
public ActionResult CropImage(ChangeAvatar model) { var imageName = model.ImageLink.Substring(model.ImageLink.LastIndexOf('/') + 1, model.ImageLink.Length - 1 - model.ImageLink.LastIndexOf('/')); var bitmap = new Bitmap(Server.MapPath("~/Tmp/" + imageName)); double ratio = (float)bitmap.Width / 400; var cropRect = new Rectangle((int)Math.Round(model.X * ratio), (int)Math.Round(model.Y * ratio), (int)Math.Round(model.Width * ratio), (int)Math.Round(model.Height * ratio)); var target = new Bitmap(cropRect.Width, cropRect.Height); using (Graphics g = Graphics.FromImage(target)) { g.DrawImage(bitmap, new Rectangle(0, 0, target.Width, target.Height), cropRect, GraphicsUnit.Pixel); } var mem = new MemoryStream(); target.Save(mem, ImageFormat.Jpeg); bitmap.Dispose(); //Delete tmp file System.IO.File.Delete(Server.MapPath("~/Tmp/" + imageName)); imageName = imageName.Substring(0, imageName.LastIndexOf('.')) + ".jpg"; var member = _member.ChangeAvatar(SessionManager.UserLogged.UserId, imageName, Convert.ToBase64String(mem.ToArray())); if (member.Code == "-1") return View(new ChangeAvatar()); return RedirectToAction("Manage"); }
public ActionResult CropImage() { if (_globalAvatar == null) { return RedirectToAction("ChangeAvatar"); } var model = _globalAvatar; _globalAvatar = null; return View(model); }
public ActionResult ChangeAvatar(ChangeAvatar model) { try { if (model.FileImage.ContentLength > 0) { model.FileImage.SaveAs(Server.MapPath("~/Tmp/" + model.FileImage.FileName)); model.ImageLink = "/Tmp/" + model.FileImage.FileName; model.Type = false; _globalAvatar = model; return RedirectToAction("CropImage"); } return View(model); } catch (Exception exception) { Logger.Error(exception.Message + exception.InnerException.Message); return View(new ChangeAvatar(){ImageLink = exception.Message + exception.InnerException.Message}); } }