Removing EXIF Does not work when using Different Encoders to Choose Format

Im using the following code to fix the orientation of image taking into account the EXIF Orientation Tag

 static void FixImageOrientation(Image srce) { const int ExifOrientationId = 0x112; // Read orientation tag if (!srce.PropertyIdList.Contains(ExifOrientationId)) return; var prop = srce.GetPropertyItem(ExifOrientationId); var orient = BitConverter.ToInt16(prop.Value, 0); // Force value to 1 prop.Value = BitConverter.GetBytes((short)1); srce.SetPropertyItem(prop); // Rotate/flip image according to <orient> switch (orient) { case 1: srce.RotateFlip(RotateFlipType.RotateNoneFlipNone); break; case 2: srce.RotateFlip(RotateFlipType.RotateNoneFlipX); break; case 3: srce.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case 4: srce.RotateFlip( RotateFlipType.Rotate180FlipX); break; case 5: srce.RotateFlip(RotateFlipType.Rotate90FlipX); break; case 6: srce.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case 7: srce.RotateFlip(RotateFlipType.Rotate270FlipX); break; case 8: srce.RotateFlip(RotateFlipType.Rotate270FlipNone); break; default: srce.RotateFlip(RotateFlipType.RotateNoneFlipNone); break; } } 

This code removes the EXIF Orientation Tag properly. And saving the image works is i simply use img.save

But the app provides user the ability to select the format of the image.For that i use the following code

 private void saveJpeg(string path, Bitmap img, long quality) { EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); ImageCodecInfo Codec = this.getEncoderInfo(imgformat); if (Codec == null) return; EncoderParameters encoderParams = new EncoderParameters(1); encoderParams.Param[0] = qualityParam; img.Save(path + ext, Codec, encoderParams); } public string getimgext(string ccodec) { if (ccodec.Equals("image/png")) { return ".png"; } else if (ccodec.Equals("image/jpeg")) { return ".jpg"; } else if (ccodec.Equals("image/tiff")) { return ".tif"; } else if (ccodec.Equals("image/bmp")) { return ".bmp"; } else if (ccodec.Equals("image/gif")) { return ".gif"; } else { return null; } } private ImageCodecInfo getEncoderInfo(string mimeType) { // Get image codecs for all image formats ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); // Find the correct image codec for (int i = 0; i < codecs.Length; i++) if (codecs[i].MimeType == mimeType) return codecs[i]; return null; } 

When i save the image with SaveJpeg the image gets saved with wrong orientation.What im i doing wrong? Please help.

by maxer111 via /r/csharp

Leave a Reply