private void refreshImages() { Cursor.Current = Cursors.WaitCursor; if (!validateFolder(currentFolder)) { lvwImages.Items.Clear(); return; } tbxFolder.Text = currentFolder; try { lvwImages.Items.Clear(); foreach (string filename in Directory.GetFiles(currentFolder, "*.jpg")) { FileInfo fi = new FileInfo(filename); ListViewItem lvi = new ListViewItem(fi.Name); lvi.SubItems.Add(ExifImageInfo.GetOrientation(filename).ToString()); lvwImages.Items.Add(lvi); } } finally { Cursor.Current = Cursors.Default; } }
private void rotateImage(string filename) { Image image; EncoderValue encoderValue; ImageCodecInfo codec = retrieveCodec("image/jpeg"); switch (ExifImageInfo.GetOrientation(filename)) { case Orientation.Left: encoderValue = EncoderValue.TransformRotate90; break; case Orientation.Right: encoderValue = EncoderValue.TransformRotate270; break; default: return; } EncoderParameters parameters = new EncoderParameters(1); parameters.Param[0] = new EncoderParameter(Encoder.Transformation, (long)encoderValue); MemoryStream memoryStream = new MemoryStream(); using (image = Image.FromFile(filename)) { // Update the Exif orientation data in the image int index = Array.IndexOf(image.PropertyIdList, ExifTags.Orientation); PropertyItem propertyItem = image.PropertyItems[index]; propertyItem.Value = BitConverter.GetBytes((int)Orientation.Straight); image.SetPropertyItem(propertyItem); // Save image out to a memory stream that we'll later write to disk image.Save(memoryStream, codec, parameters); } // Write the memory stream to disk Image newImage = new Bitmap(memoryStream); newImage.Save(filename); }