diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs
index 0125d2703..183076ac5 100644
--- a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs
+++ b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs
@@ -427,10 +427,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort
{
if (this.isExif)
{
- ExifValue horizontal = this.MetaData.ExifProfile.GetValue(ExifTag.XResolution);
- ExifValue vertical = this.MetaData.ExifProfile.GetValue(ExifTag.YResolution);
- double horizontalValue = horizontal != null ? ((Rational)horizontal.Value).ToDouble() : 0;
- double verticalValue = vertical != null ? ((Rational)vertical.Value).ToDouble() : 0;
+ double horizontalValue = this.MetaData.ExifProfile.TryGetValue(ExifTag.XResolution, out var horizonalTag)
+ ? ((Rational)horizonalTag.Value).ToDouble()
+ : 0;
+
+ double verticalValue = this.MetaData.ExifProfile.TryGetValue(ExifTag.YResolution, out var verticalTag)
+ ? ((Rational)verticalTag.Value).ToDouble()
+ : 0;
if (horizontalValue > 0 && verticalValue > 0)
{
diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs
index a600658b0..ba5362993 100644
--- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs
+++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs
@@ -380,10 +380,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort
{
if (this.isExif)
{
- ExifValue horizontal = image.MetaData.ExifProfile.GetValue(ExifTag.XResolution);
- ExifValue vertical = image.MetaData.ExifProfile.GetValue(ExifTag.YResolution);
- double horizontalValue = horizontal != null ? ((Rational)horizontal.Value).ToDouble() : 0;
- double verticalValue = vertical != null ? ((Rational)vertical.Value).ToDouble() : 0;
+ double horizontalValue = image.MetaData.ExifProfile.TryGetValue(ExifTag.XResolution, out var horizontalTag)
+ ? ((Rational)horizontalTag.Value).ToDouble()
+ : 0;
+
+ double verticalValue = image.MetaData.ExifProfile.TryGetValue(ExifTag.YResolution, out var verticalTag)
+ ? ((Rational)verticalTag.Value).ToDouble()
+ : 0;
if (horizontalValue > 0 && verticalValue > 0)
{
diff --git a/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs b/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs
index de70c41f5..4178bcec8 100644
--- a/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs
+++ b/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs
@@ -160,6 +160,31 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
return null;
}
+ ///
+ /// Conditionally the value of the tag if it exists.
+ ///
+ /// The tag of the EXIF value.
+ /// The value of the tag, if found.
+ ///
+ /// The .
+ ///
+ public bool TryGetValue(ExifTag tag, out ExifValue value)
+ {
+ foreach (ExifValue exifValue in this.Values)
+ {
+ if (exifValue.Tag == tag)
+ {
+ value = exifValue;
+
+ return true;
+ }
+ }
+
+ value = default;
+
+ return false;
+ }
+
///
/// Removes the value with the specified tag.
///
diff --git a/src/ImageSharp/MetaData/Profiles/Exif/ExifValue.cs b/src/ImageSharp/MetaData/Profiles/Exif/ExifValue.cs
index 6367cbb83..bdd902e23 100644
--- a/src/ImageSharp/MetaData/Profiles/Exif/ExifValue.cs
+++ b/src/ImageSharp/MetaData/Profiles/Exif/ExifValue.cs
@@ -691,8 +691,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
/// The
private string ToString(object value)
{
- string description = ExifTagDescriptionAttribute.GetDescription(this.Tag, value);
- if (description != null)
+ if (ExifTagDescriptionAttribute.GetDescription(this.Tag, value) is string description)
{
return description;
}