public static void rotateCheckingPreview(ref System.Drawing.Image imgPreview, string path) { var directories = MetadataExtractor.ImageMetadataReader.ReadMetadata(path); string orientationTag = ""; foreach (var directory in directories) { foreach (var tag in directory.Tags) { if (tag.Name == "Orientation" && directory.Name == "Exif IFD0") { orientationTag = tag.Description; break; } } if (directory.HasError) { foreach (var error in directory.Errors) { Console.WriteLine($"ERROR: {error}"); } } } Console.WriteLine(orientationTag); Regex rx = new Regex(@"rotate\s+[0-9]{1,3}(cw|ccw|\s){0,1}", RegexOptions.Compiled | RegexOptions.IgnoreCase); Regex rx2 = new Regex(@"(cw|ccw)", RegexOptions.Compiled | RegexOptions.IgnoreCase); Regex rx3 = new Regex(@"cw", RegexOptions.Compiled | RegexOptions.IgnoreCase); if (rx.IsMatch(orientationTag)) { Match matchDegree = Regex.Match(orientationTag, @"[0-9]{1,3}"); Match matchType = rx2.Match(orientationTag); degree = double.Parse(matchDegree.Value.ToString()); if (!rx3.IsMatch(matchType.Value)) { degree = -degree; } switch (degree) { case 90: imgPreview.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone); break; case 180: imgPreview.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); break; case 270: imgPreview.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone); break; case -90: imgPreview.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone); break; case -180: imgPreview.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); break; case -270: imgPreview.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone); break; } } }
private void listBoxImageFile_MouseClick(object sender, MouseEventArgs e) { int count = 0; foreach (ImageSelection img in listBoxImageFile.SelectedItems) { count++; } if (count == 1) { foreach (ImageSelection img in listImages) { if (img.ToString() == listBoxImageFile.SelectedItem.ToString()) { System.Drawing.Image imgPreview = System.Drawing.Image.FromFile(img.getPath()); OrientationChecking.rotateCheckingPreview(ref imgPreview, img.getPath()); pictureBoxPreview.Image = imgPreview; break; } } } else { pictureBoxPreview.Image = null; } }