diff --git a/src/modules/imageresizer/ui/Extensions/BitmapMetadataExtension.cs b/src/modules/imageresizer/ui/Extensions/BitmapMetadataExtension.cs
index 6bf4b4ace9..97fd4ebdba 100644
--- a/src/modules/imageresizer/ui/Extensions/BitmapMetadataExtension.cs
+++ b/src/modules/imageresizer/ui/Extensions/BitmapMetadataExtension.cs
@@ -187,6 +187,31 @@ namespace ImageResizer.Extensions
}
}
+ ///
+ /// Detect whether the metadata object is valid and can be copied successfully
+ ///
+ ///
+ /// ImageMetadata.Clone() causes an exception if there is something wrong with the metadata object.
+ /// Operation is rather expensive.
+ ///
+ /// Metadata object to be checked
+ /// true if valid, false if invalid
+ public static bool IsMetadataObjectValid(this BitmapMetadata metadata)
+ {
+ try
+ {
+ _ = metadata.Clone();
+
+ return true;
+ }
+#pragma warning disable CA1031 // Do not catch general exception types
+ catch (Exception)
+#pragma warning restore CA1031 // Do not catch general exception types
+ {
+ return false;
+ }
+ }
+
///
/// Prints all metadata to debug console
///
diff --git a/src/modules/imageresizer/ui/Models/ResizeOperation.cs b/src/modules/imageresizer/ui/Models/ResizeOperation.cs
index daf5b3137b..0d087193bf 100644
--- a/src/modules/imageresizer/ui/Models/ResizeOperation.cs
+++ b/src/modules/imageresizer/ui/Models/ResizeOperation.cs
@@ -104,7 +104,15 @@ namespace ImageResizer.Models
}
}
- metadata = newMetadata;
+ if (newMetadata.IsMetadataObjectValid())
+ {
+ metadata = newMetadata;
+ }
+ else
+ {
+ // Seems like we build an invalid metadata object. ImageResizer will fail when trying to write the image to disk. We discard all metadata to be able to save the image.
+ metadata = null;
+ }
}
catch (ArgumentException ex)
{