void DeletePicture(string name, string size) { var path = Server.MapPath(Url.AccountPicture(name, size)); try { System.IO.File.Delete(path); } catch { } }
public ActionResult ChangePicture(Guid userId, string returnUrl) { try { if (Request.Files.Count == 1 && Request.Files[0].ContentLength < 562144) { ProfileRepository profileRepository = new ProfileRepository(); Profile userProfile = profileRepository.GetUserProfile(userId); var pictureName = new StringBuilder(12).AppendRandomString(12).ToString(); var default128x128 = Server.MapPath(Url.AccountPicture(pictureName, "128x128")); var default64x64 = Server.MapPath(Url.AccountPicture(pictureName, "64x64")); var default32x32 = Server.MapPath(Url.AccountPicture(pictureName, "32x32")); var default16x16 = Server.MapPath(Url.AccountPicture(pictureName, "16x16")); while (System.IO.File.Exists(default128x128) || System.IO.File.Exists(default64x64) || System.IO.File.Exists(default32x32) || System.IO.File.Exists(default16x16)) { pictureName = new StringBuilder(12).AppendRandomString(12).ToString(); default128x128 = Server.MapPath(Url.AccountPicture(pictureName, "128x128")); default64x64 = Server.MapPath(Url.AccountPicture(pictureName, "64x64")); default32x32 = Server.MapPath(Url.AccountPicture(pictureName, "32x32")); default16x16 = Server.MapPath(Url.AccountPicture(pictureName, "16x16")); } using (var srcImage = Image.FromStream(Request.Files[0].InputStream)) { using (var destImage = srcImage.ResizeTo(128, 128)) { destImage.Save(default128x128, ImageFormat.Png); } using (var destImage = srcImage.ResizeTo(64, 64)) { destImage.Save(default64x64, ImageFormat.Png); } using (var destImage = srcImage.ResizeTo(32, 32)) { destImage.Save(default32x32, ImageFormat.Png); } using (var destImage = srcImage.ResizeTo(16, 16)) { destImage.Save(default16x16, ImageFormat.Png); } } if (userProfile.PictureName != null) { DeletePicture(userProfile.PictureName, "128x128"); DeletePicture(userProfile.PictureName, "64x64"); DeletePicture(userProfile.PictureName, "32x32"); DeletePicture(userProfile.PictureName, "16x16"); } userProfile.PictureName = pictureName; profileRepository.Save(); } TempData["message"] = "New profile picture was uploaded and activated successfully"; if (!string.IsNullOrEmpty(returnUrl)) { return(Redirect(returnUrl)); } return(RedirectToAction("MyAccount")); } catch (Exception) { throw; } }