public ActionResult EditApartmentPhoto(EditApartmentPhotoViewResult newApartment) { if (ModelState.IsValid) { if (newApartment.InputImages != null) { Apartment apartment = _apartmentRepository.GetApartment(newApartment.ApartmentId); var Photos = _apartmentRepository.GetApartmentPhotos(apartment); var HasDafaultPhoto = true; if (Photos.Count == 0) { HasDafaultPhoto = false; } foreach (var photo in newApartment.InputImages.Where(m => m != null)) { int height, width, max; int centerx, centery; int offsetx, offsety; using (System.Drawing.Image image = System.Drawing.Image.FromStream(photo.InputStream, true, true)) { height = image.Height; width = image.Width; centerx = width / 2; centery = height / 2; if (height > width) { max = width; offsetx = 0; offsety = centery - max / 2; } else { max = height; offsety = 0; offsetx = centerx - max / 2; } } ApartmentPhoto Photo = new ApartmentPhoto { ApartmentId = newApartment.ApartmentId, Links = new List <ApartmentPhotoLink>() }; Photo.Links.Add(new ApartmentPhotoLink { Link = PreviewCreator.SaveImage(photo.InputStream), TypeId = 1 }); Photo.Links.Add(new ApartmentPhotoLink { Link = PreviewCreator.SaveImageAndResize(photo.InputStream, (int)offsetx, (int)offsety, (int)max, (int)max, new System.Drawing.Size(550, 550)), TypeId = 2 }); Photo.Links.Add(new ApartmentPhotoLink { Link = PreviewCreator.SaveImageAndResize(photo.InputStream, (int)offsetx, (int)offsety, (int)max, (int)max, new System.Drawing.Size(100, 100)), TypeId = 3 }); _apartmentRepository.AddApartmentPhoto(apartment, Photo); if (!HasDafaultPhoto) { _apartmentRepository.SetApartmentDefaultPhoto(apartment, Photo); } HasDafaultPhoto = true; TempData["toastrMessage"] = String.Format("Квартира изменена"); TempData["toastrType"] = "success"; } } } else { } return(RedirectToAction("EditApartmentPhoto")); }
public ActionResult ProfileEditAvatar(ProfileEditResult newProfile, double?cropx = 0, double?cropy = 0, double?croph = 0, double?cropw = 0) { if ((ModelState.IsValid)) { if (newProfile.inputImage != null) { newProfile.inputImage = newProfile.inputImage ?? Request.Files["inputImage"]; if (newProfile.inputImage != null) { var extension = Path.GetExtension(newProfile.inputImage.FileName); if (!string.IsNullOrWhiteSpace(extension)) { if (newProfile.Profile.ImageLink != null) { PreviewCreator.RemoveImage(System.Web.Hosting.HostingEnvironment.MapPath(newProfile.Profile.ImageLink)); } if (newProfile.Profile.ImageAvatarLink != null) { PreviewCreator.RemoveImage(System.Web.Hosting.HostingEnvironment.MapPath(newProfile.Profile.ImageAvatarLink)); } if (newProfile.Profile.ImageAvatarBigLink != null) { PreviewCreator.RemoveImage(System.Web.Hosting.HostingEnvironment.MapPath(newProfile.Profile.ImageAvatarBigLink)); } newProfile.Profile.ImageType = ("image/" + extension).Replace(".", ""); newProfile.Profile.ImageLink = PreviewCreator.SaveImage(newProfile.inputImage.InputStream); newProfile.Profile.ImageAvatarType = ("image/" + ".jpg").Replace(".", ""); newProfile.Profile.ImageAvatarLink = PreviewCreator.SaveImageAndResize(newProfile.inputImage.InputStream, (int)cropx, (int)cropy, (int)cropw, (int)croph, new Size(48, 48)); newProfile.Profile.ImageAvatarBigType = ("image/" + ".jpg").Replace(".", ""); newProfile.Profile.ImageAvatarBigLink = PreviewCreator.SaveImageAndResize(newProfile.inputImage.InputStream, (int)cropx, (int)cropy, (int)cropw, (int)croph, new Size(550, 550)); } } } else { if ((newProfile.Profile.ImageLink != null) && (cropx != 0) && (cropy != 0) && (cropw != 0) && (croph != 0)) { using (var fileStream = new FileStream(Server.MapPath(newProfile.Profile.ImageLink), FileMode.Open, FileAccess.Read)) { if (newProfile.Profile.ImageAvatarLink != null) { PreviewCreator.RemoveImage(System.Web.Hosting.HostingEnvironment.MapPath(newProfile.Profile.ImageAvatarLink)); } if (newProfile.Profile.ImageAvatarBigLink != null) { PreviewCreator.RemoveImage(System.Web.Hosting.HostingEnvironment.MapPath(newProfile.Profile.ImageAvatarBigLink)); } newProfile.Profile.ImageAvatarType = ("image/" + ".jpg").Replace(".", ""); newProfile.Profile.ImageAvatarLink = PreviewCreator.SaveImageAndResize(fileStream, (int)cropx, (int)cropy, (int)cropw, (int)croph, new Size(48, 48)); newProfile.Profile.ImageAvatarBigType = ("image/" + ".jpg").Replace(".", ""); newProfile.Profile.ImageAvatarBigLink = PreviewCreator.SaveImageAndResize(fileStream, (int)cropx, (int)cropy, (int)cropw, (int)croph, new Size(550, 550));; } } } newProfile.Profile.New = false; _profileService.UpdateProfile(newProfile.Profile, ProfileUpdateMode.Avatar); UpdateProfileMEssage(); } return(View(newProfile)); }