使用不同的编码器选择格式时删除 EXIF 不起作用

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

考虑到 EXIF 方向标签,我正在使用以下代码来修复图像的方向

 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;
            }
        }

此代码正确删除了 EXIF 方向标签。 保存图像是我简单地使用 img.save

但是该应用程序为用户提供了select image.For 格式的能力,我使用以下代码

 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;
        }

当我用 SaveJpeg 保存图片时,图片保存错误 orientation.What 我做错了吗?请帮忙。

更新:

我修改了方法,这样我就不需要创建位图的新实例,因为循环处理很多 files.But 这不起作用,除非我创建 bitmap.This 的新实例与旧版本相比,该过程额外消耗了 10 多秒的处理时间。 我正在使用这样的代码

image = (Bitmap)FixImageOrientation(Bitmap.FromFile(path));


Image FixImageOrientation(Image srce)
        {
            const int ExifOrientationId = 0x112;
            // Read orientation tag
            if (!srce.PropertyIdList.Contains(ExifOrientationId)) return srce;
            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);
                    return srce;
                    

                case 2:
                    srce.RotateFlip(RotateFlipType.RotateNoneFlipX);
                    return srce;

                case 3:
                    srce.RotateFlip(RotateFlipType.Rotate180FlipNone);
                    return srce;

                case 4:
                    srce.RotateFlip(RotateFlipType.Rotate180FlipX);
                    return srce;

                case 5:
                    srce.RotateFlip(RotateFlipType.Rotate90FlipX);
                    return srce;

                case 6:
                    srce.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    return srce;

                case 7:
                    srce.RotateFlip(RotateFlipType.Rotate270FlipX);
                    return srce;

                case 8:
                    srce.RotateFlip(RotateFlipType.Rotate270FlipNone);
                    return srce;

                default:
                    srce.RotateFlip(RotateFlipType.RotateNoneFlipNone);
                    return srce;
            }
        }

在一种方法中,您有 Image 类型的参数,但在其他方法中,您有 Bitmap 类型的参数。所以我 猜测 您在调用 FixImageOrientation 之前将源 JPEG 图像转换为位图 FixImageOrientation

这会起作用:

 var jpeg = Image.FromFile("original.jpg");
 FixImageOrientation(jpeg);
 var bitmap = new Bitmap(jpeg);
 saveJpeg("new",bitmap,1);

这不会:

var jpeg = Image.FromFile("original.jpg");
var bitmap = new Bitmap(jpeg);
FixImageOrientation(bitmap);
saveJpeg("new", bitmap, 1);