static void imgStyle(ref Image img, ImageSelection image) { var directories = MetadataExtractor.ImageMetadataReader.ReadMetadata(image.getPath()); foreach (var directory in directories) { foreach (var tag in directory.Tags) { if (tag.Name == "Orientation" && directory.Name == "Exif IFD0") { OrientationChecking.rotateChecking(ref img, tag.Description); break; } } if (directory.HasError) { foreach (var error in directory.Errors) { Console.WriteLine($"ERROR: {error}"); } } } img.SetHorizontalAlignment(HorizontalAlignment.CENTER); img.SetAutoScale(true); }
private void addFiles_Click(object sender, EventArgs e) { string[] pathToImages; string[] fileNames; ImageSelection currentImageSelection; OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Multiselect = true; openFileDialog.Filter = "Image file| " + " *.png; *.jpg; *.jpeg; *.jfif; *.bmp; *.tif; *.tiff; *.gif"; if (openFileDialog.ShowDialog() == DialogResult.OK) { pathToImages = openFileDialog.FileNames.ToArray(); fileNames = openFileDialog.SafeFileNames.ToArray(); for (int i = 0; i < fileNames.Length; i++) { if (checkingDuplicates(fileNames.ElementAt(i))) { currentImageSelection = new ImageSelection(fileNames.ElementAt(i), pathToImages.ElementAt(i)); Dimensions.getDim(ref currentImageSelection); listImages.Add(currentImageSelection); listBoxImageFile.Items.Add(listImages.Last()); } else { MessageBox.Show("An image with this name already exists. Please rename or choose another image !"); } } labelNrImages.Text = "Images : " + listImages.Count; labelFullPages.Text = "Full Pages : " + (listImages.Count / imgNumber).ToString(); } }
public static void rotateChecking(ref Image img, string orientationTag, ref ImageSelection image, bool setOrientation) { 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; } Console.WriteLine(image.ToString() + " " + orientationTag + "appear " + image.format); switch (degree) { case 90: img.SetRotationAngle(-Math.PI / 2); if (!setOrientation) { image.format = (image.format == "portrait") ? "landscape" : "portrait"; } break; case 180: img.SetRotationAngle(-Math.PI); break; case 270: img.SetRotationAngle(-Math.PI * 3 / 2); if (!setOrientation) { image.format = (image.format == "portrait") ? "landscape" : "portrait"; } break; case -90: img.SetRotationAngle(Math.PI / 2); if (!setOrientation) { image.format = (image.format == "portrait") ? "landscape" : "portrait"; } break; case -180: img.SetRotationAngle(Math.PI); break; case -270: img.SetRotationAngle(Math.PI * 3 / 2); if (!setOrientation) { image.format = (image.format == "portrait") ? "landscape" : "portrait"; } break; } } }
public static void getDim(ref ImageSelection image) { var directories = MetadataExtractor.ImageMetadataReader.ReadMetadata(image.getPath()); bool foundWidth = false; bool foundHeight = false; string descriptionWidth = ""; string descriptionHeight = ""; foreach (var directory in directories) { foreach (var tag in directory.Tags) { if (!foundWidth && (tag.Name == "Image Width") && (directory.Name == "JPEG" || directory.Name == "JPG" || directory.Name.Contains("JPG") || directory.Name.Contains("JPEG") || directory.Name.Contains("PNG") || directory.Name.Contains("JFIF") || directory.Name.Contains("BMP") || directory.Name.Contains("TIF") || directory.Name.Contains("TIFF") || directory.Name.Contains("GIF"))) { descriptionWidth = tag.Description; foundWidth = true; } if (!foundHeight && (tag.Name == "Image Height") && (directory.Name == "JPEG" || directory.Name == "JPG" || directory.Name.Contains("JPG") || directory.Name.Contains("JPEG") || directory.Name.Contains("PNG") || directory.Name.Contains("JFIF") || directory.Name.Contains("BMP") || directory.Name.Contains("TIF") || directory.Name.Contains("TIFF") || directory.Name.Contains("GIF"))) { descriptionHeight = tag.Description; foundHeight = true; } if (foundHeight && foundWidth) { break; } } if (directory.HasError) { foreach (var error in directory.Errors) { Console.WriteLine($"ERROR: {error}"); } } if (foundHeight && foundWidth) { break; } } Regex rx = new Regex(@"[0-9]{1,6}", RegexOptions.Compiled | RegexOptions.IgnoreCase); if (rx.IsMatch(descriptionWidth)) { image.setWidth(int.Parse(rx.Match(descriptionWidth).Value)); } if (rx.IsMatch(descriptionHeight)) { image.setHeight(int.Parse(rx.Match(descriptionHeight).Value)); } if (image.getHeight() >= image.getWidth()) { image.format = "portrait"; } else { image.format = "landscape"; } Console.WriteLine(image.ToString() + " " + image.getWidth() + "(width)x" + image.getHeight() + "(height) " + image.format); }