public ActionResult Edit([Bind(Include = "PhotoGuid,SpotGuid,Description,Longitude,Latitude,File")] PhotoEditViewModel photoEditViewModel) { if (ModelState.IsValid) { Photo photo = db.Photos.Find(photoEditViewModel.PhotoGuid); photo.PhotoGuid = photoEditViewModel.PhotoGuid; photo.Spot = photoEditViewModel.Spot; photo.Description = photoEditViewModel.Description; photo.Longitude = photoEditViewModel.Longitude; photo.Latitude = photoEditViewModel.Latitude; photo.DateModified = DateTime.Now; photo.UserModifiedID = Auxiliaries.GetUserId(User); ViewBag.SpotGuid = new SelectList(db.Spots, "SpotGuid", "SpotName", photoEditViewModel.SpotGuid); // Handle the photo if (photoEditViewModel.File != null && photoEditViewModel.File.ContentLength > 0) { if (!Auxiliaries.ValidImageTypes.Contains(photoEditViewModel.File.ContentType)) { ModelState.AddModelError("File", "Izberite sliko, ki je v enem od naštetih formatov: GIF, JPG, ali PNG."); if (photo.File != null && photo.File.Length > 0) { photoEditViewModel.File = new MemoryPostedFile(photo.File); var base64 = Convert.ToBase64String(photo.File); var imgSrc = String.Format("data:image/gif;base64,{0}", base64); ViewBag.ImgSrc = imgSrc; } return(View(photoEditViewModel)); } else { using (var reader = new BinaryReader(photoEditViewModel.File.InputStream)) { photo.File = reader.ReadBytes(photoEditViewModel.File.ContentLength); int thumbWidth = 250; int thumbHeight = 200; MemoryStream myMemStream = new MemoryStream(photo.File); System.Drawing.Image fullsizeImage = System.Drawing.Image.FromStream(myMemStream); System.Drawing.Image newImage = fullsizeImage.GetThumbnailImage(thumbWidth, thumbHeight, null, IntPtr.Zero); MemoryStream myResult = new MemoryStream(); newImage.Save(myResult, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] myResultByte = myResult.ToArray(); photo.Thumbnail = myResultByte; } } } db.Entry(photo).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(photoEditViewModel)); }
public void DeletePhoto(int photoId) { PhotoEntity photoEntity = _photosContext.Photos.Where(photo => photo.Id == photoId).Single(); _photosContext.Entry(photoEntity).State = System.Data.Entity.EntityState.Deleted; _photosContext.SaveChanges(); RemoveAlbumIfEmpty(photoEntity.AlbumId); }