public ActionResult UploadPicture(HttpPostedFileBase file, int?selectedId) { if (file == null) { return(RedirectToAction("Index", new { id = selectedId })); } using (PicturesGalleryEntities context = new PicturesGalleryEntities()) { PictureDbEntity newPicture = new PictureDbEntity { Name = file.FileName, Description = "Sample picture description" }; context.PictureDbEntities.Add(newPicture); context.SaveChanges(); // Use Id from database as a file name because it's unique for each picture string path = Path.Combine(Server.MapPath("~/Images"), newPicture.Id + Path.GetExtension(file.FileName)); file.SaveAs(path); } return(RedirectToAction("Index", new { id = selectedId })); }
private void FillImageMetadata(int id, ImageMetaData metaData) { using (PicturesGalleryEntities context = new PicturesGalleryEntities()) { PictureDbEntity picture = context.PictureDbEntities.Single(p => p.Id == id); string filePath = Path.Combine(Server.MapPath("~/Images"), picture.Id + Path.GetExtension(picture.Name)); Image image = Image.FromFile(filePath, false); PropertyItem[] propertyItems = image.PropertyItems; List <int> ids = new List <int> { 0x010F, 0x0110, 0x829A, 0x0132, 0x0103, 0x9000 }; if (!propertyItems.Select(i => i.Id).Intersect(ids).Any()) { return; } metaData.MetadataExists = "true"; PropertyItem manufacturerPropertyItem = propertyItems.SingleOrDefault(i => i.Id == 0x010F); if (manufacturerPropertyItem != null) { metaData.EquipmentManufacturer = new ASCIIEncoding().GetString(manufacturerPropertyItem.Value); } PropertyItem modelPropertyItem = propertyItems.SingleOrDefault(i => i.Id == 0x0110); if (modelPropertyItem != null) { metaData.EquipmentModel = new ASCIIEncoding().GetString(modelPropertyItem.Value); } PropertyItem exposureTimePropertyItem = propertyItems.SingleOrDefault(i => i.Id == 0x829A); if (exposureTimePropertyItem != null) { metaData.ExposureTime = GetExposureTimeFromBytes(exposureTimePropertyItem.Value); } PropertyItem dateTakenPropertyItem = propertyItems.SingleOrDefault(i => i.Id == 0x0132); if (dateTakenPropertyItem != null) { metaData.DateTaken = new ASCIIEncoding().GetString(dateTakenPropertyItem.Value); } PropertyItem exifVersionPropertyItem = propertyItems.SingleOrDefault(i => i.Id == 0x9000); if (exifVersionPropertyItem != null) { metaData.ExifVersion = new ASCIIEncoding().GetString(exifVersionPropertyItem.Value).Insert(2, ".").Trim('0'); } PropertyItem latitudePropertyItem = propertyItems.SingleOrDefault(i => i.Id == 0x0002); PropertyItem latitudeRefPropertyItem = propertyItems.SingleOrDefault(i => i.Id == 0x0001); PropertyItem longitudePropertyItem = propertyItems.SingleOrDefault(i => i.Id == 0x0004); PropertyItem longitudeRefPropertyItem = propertyItems.SingleOrDefault(i => i.Id == 0x0003); if (latitudePropertyItem != null && latitudeRefPropertyItem != null && longitudePropertyItem != null && longitudeRefPropertyItem != null) { metaData.LatitudeLongitude = GetLatitudeLongitude(latitudePropertyItem.Value, longitudePropertyItem.Value, latitudeRefPropertyItem.Value, longitudeRefPropertyItem.Value); } } }