private static void CorrectOrientation(string file, PhotoInfo.PhotoOrientation orientation) { using (var image = Image.FromFile(file)) { if (orientation == PhotoInfo.PhotoOrientation.RightTop) { image.RotateFlip(RotateFlipType.Rotate90FlipNone); } else if (orientation == PhotoInfo.PhotoOrientation.LeftBottom) { image.RotateFlip(RotateFlipType.Rotate270FlipNone); } image.Save(file, ImageFormat.Jpeg); } }
public static PhotoInfo GetPhotoInfo(string path) { const int thumbnailHeight = 200; const int idDateTaken = 36867; const int idOrientation = 274; Regex r = new Regex(":"); DateTime? dateTaken = null; PhotoInfo.PhotoOrientation orientation = PhotoInfo.PhotoOrientation.Unknown; Image thumbnail; using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) using (Image myImage = Image.FromStream(fs, false, false)) { double format = (double)myImage.Size.Width / (double)myImage.Size.Height; int thumbnailWidth = (int)(thumbnailHeight * format); thumbnail = myImage.GetThumbnailImage(thumbnailWidth, thumbnailHeight, null, IntPtr.Zero); if (myImage.PropertyIdList.Contains(idDateTaken)) { PropertyItem propDateTaken = myImage.GetPropertyItem(idDateTaken); string dateTakenText = r.Replace(Encoding.UTF8.GetString(propDateTaken.Value), "-", 2); dateTaken = DateTime.Parse(dateTakenText); } if (myImage.PropertyIdList.Contains(idOrientation)) { var propOrientation = myImage.GetPropertyItem(idOrientation); orientation = (PhotoInfo.PhotoOrientation)propOrientation.Value[0]; } } return(new PhotoInfo() { DateTaken = dateTaken.Value, Orientation = orientation, Thumbnail = thumbnail }); }