mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-03 09:46:54 +02:00
[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.
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user