From 61805aada23467d3185e91a6d154d03155acfdef Mon Sep 17 00:00:00 2001 From: Adam Childers Date: Tue, 24 May 2022 10:58:38 -0400 Subject: [PATCH] [ImageResizer]Don't update metadata if image wasn't changed (#17880) * Update ResizeOperation.cs Add a new boolean to check if the image was resized, skip modifying the metadata if the image was not modified. * Simplify code to prevent un-needed metadata work when the image isn't changed Simplify code to prevent un-needed metadata work when the image isn't changed. Thanks Crutkas for the guidance. --- .../imageresizer/ui/Models/ResizeOperation.cs | 45 +++++++++++-------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/src/modules/imageresizer/ui/Models/ResizeOperation.cs b/src/modules/imageresizer/ui/Models/ResizeOperation.cs index 12da781f6f..eed54e1c47 100644 --- a/src/modules/imageresizer/ui/Models/ResizeOperation.cs +++ b/src/modules/imageresizer/ui/Models/ResizeOperation.cs @@ -75,29 +75,38 @@ namespace ImageResizer.Models foreach (var originalFrame in decoder.Frames) { var transformedBitmap = Transform(originalFrame); - BitmapMetadata originalMetadata = (BitmapMetadata)originalFrame.Metadata; -#if DEBUG - Debug.WriteLine($"### Processing metadata of file {_file}"); - originalMetadata.PrintsAllMetadataToDebugOutput(); -#endif - - var metadata = GetValidMetadata(originalMetadata, transformedBitmap, containerFormat); - - if (_settings.RemoveMetadata && metadata != null) + // if the frame was not modified, we should not replace the metadata + if (transformedBitmap == originalFrame) { - // strip any metadata that doesn't affect rendering - var newMetadata = new BitmapMetadata(metadata.Format); - - metadata.CopyMetadataPropertyTo(newMetadata, "System.Photo.Orientation"); - metadata.CopyMetadataPropertyTo(newMetadata, "System.Image.ColorSpace"); - - metadata = newMetadata; + encoder.Frames.Add(originalFrame); } + else + { + BitmapMetadata originalMetadata = (BitmapMetadata)originalFrame.Metadata; - var frame = CreateBitmapFrame(transformedBitmap, metadata); + #if DEBUG + Debug.WriteLine($"### Processing metadata of file {_file}"); + originalMetadata.PrintsAllMetadataToDebugOutput(); + #endif - encoder.Frames.Add(frame); + var metadata = GetValidMetadata(originalMetadata, transformedBitmap, containerFormat); + + if (_settings.RemoveMetadata && metadata != null) + { + // strip any metadata that doesn't affect rendering + var newMetadata = new BitmapMetadata(metadata.Format); + + metadata.CopyMetadataPropertyTo(newMetadata, "System.Photo.Orientation"); + metadata.CopyMetadataPropertyTo(newMetadata, "System.Image.ColorSpace"); + + metadata = newMetadata; + } + + var frame = CreateBitmapFrame(transformedBitmap, metadata); + + encoder.Frames.Add(frame); + } } path = GetDestinationPath(encoder);