public ActionResult Create([Bind(Include = "PhotoGuid,SpotGuid,Description,Longitude,Latitude,File")] PhotoViewModel photoViewModel) { if (ModelState.IsValid) { Photo photo = new Photo(); photo.PhotoGuid = Guid.NewGuid(); photo.SpotGuid = photoViewModel.SpotGuid; photo.Description = photoViewModel.Description; photo.Longitude = photoViewModel.Longitude; photo.Latitude = photoViewModel.Latitude; photo.DateCreated = DateTime.Now; photo.DateModified = DateTime.Now; photo.UserCreatedID = Auxiliaries.GetUserId(User); photo.UserModifiedID = Auxiliaries.GetUserId(User); ViewBag.SpotGuid = new SelectList(db.Spots, "SpotGuid", "SpotName", photoViewModel.SpotGuid); // Handle the icon if (photoViewModel.File != null && photoViewModel.File.ContentLength > 0) { if (!Auxiliaries.ValidImageTypes.Contains(photoViewModel.File.ContentType)) { ModelState.AddModelError("File", "Izberite sliko, ki je v enem od naštetih formatov: GIF, JPG, ali PNG."); return(View(photoViewModel)); } else { using (var reader = new BinaryReader(photoViewModel.File.InputStream)) { photo.File = reader.ReadBytes(photoViewModel.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.Photos.Add(photo); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(photoViewModel)); }
public void TestAddNewPhotoAlbumToDatabase2() { var place = new PlaceEntity { Name = "TestPlace", City = "City", Country = "Country" }; var photoAlbum = new AlbumEntity { Title = "TestPhotoAlbum", Description = "TestDescription", Period = new DateTimePeriod(), Place = place, Photos = new List <PhotoEntity> { new PhotoEntity { Title = "TestPhoto#1", Image = new byte[] { 1 }, Place = place }, new PhotoEntity { Title = "TestPhoto#2", Image = new byte[] { 2 }, Place = place }, } }; using (var photosContext = new PhotosDbContext()) { //photosContext.Configuration.AutoDetectChangesEnabled = false; //photosContext.Configuration.ValidateOnSaveEnabled = false; photosContext.Albums.Add(photoAlbum); photosContext.SaveChanges(); } }
public void AddPhoto(PhotoEntity photo) { _photosContext.Photos.Add(photo); _photosContext.SaveChanges(); }