From a39ac71837145bc78265e03faa6f67974d497341 Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Sat, 21 Jan 2023 10:10:48 +0100 Subject: [PATCH] Change GetThumbnail to TryGetThumbnail --- .../Metadata/Profiles/Exif/ExifProfile.cs | 31 +++++++++++++------ .../Profiles/Exif/ExifProfileTests.cs | 7 +++-- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/ImageSharp/Metadata/Profiles/Exif/ExifProfile.cs b/src/ImageSharp/Metadata/Profiles/Exif/ExifProfile.cs index 9a41fb6a4e..ebe20cff6b 100644 --- a/src/ImageSharp/Metadata/Profiles/Exif/ExifProfile.cs +++ b/src/ImageSharp/Metadata/Profiles/Exif/ExifProfile.cs @@ -124,36 +124,47 @@ public sealed class ExifProfile : IDeepCloneable /// /// Returns the thumbnail in the EXIF profile when available. /// + /// The thumbnail /// - /// The . + /// True, if there is a thumbnail otherwise false. /// - public Image? CreateThumbnail() => this.CreateThumbnail(); + public bool TryCreateThumbnail([NotNullWhen(true)] out Image? image) + { + if (this.TryCreateThumbnail(out Image? innerimage)) + { + image = innerimage; + return true; + } + + image = null; + return false; + } /// /// Returns the thumbnail in the EXIF profile when available. /// /// The pixel format. - /// - /// The . - /// - public Image? CreateThumbnail() + /// The thumbnail. + /// True, if there is a thumbnail otherwise false. + public bool TryCreateThumbnail([NotNullWhen(true)] out Image? image) where TPixel : unmanaged, IPixel { this.InitializeValues(); - + image = null; if (this.thumbnailOffset == 0 || this.thumbnailLength == 0) { - return null; + return false; } if (this.data is null || this.data.Length < (this.thumbnailOffset + this.thumbnailLength)) { - return null; + return false; } using (var memStream = new MemoryStream(this.data, this.thumbnailOffset, this.thumbnailLength)) { - return Image.Load(memStream); + image = Image.Load(memStream); + return true; } } diff --git a/tests/ImageSharp.Tests/Metadata/Profiles/Exif/ExifProfileTests.cs b/tests/ImageSharp.Tests/Metadata/Profiles/Exif/ExifProfileTests.cs index 26f777ae72..873bc9ca2c 100644 --- a/tests/ImageSharp.Tests/Metadata/Profiles/Exif/ExifProfileTests.cs +++ b/tests/ImageSharp.Tests/Metadata/Profiles/Exif/ExifProfileTests.cs @@ -345,13 +345,14 @@ public class ExifProfileTests ExifProfile profile = GetExifProfile(); TestProfile(profile); - - using Image thumbnail = profile.CreateThumbnail(); + bool retVal = profile.TryCreateThumbnail(out Image thumbnail); + Assert.True(retVal); Assert.NotNull(thumbnail); Assert.Equal(256, thumbnail.Width); Assert.Equal(170, thumbnail.Height); - using Image genericThumbnail = profile.CreateThumbnail(); + retVal = profile.TryCreateThumbnail(out Image genericThumbnail); + Assert.True(retVal); Assert.NotNull(genericThumbnail); Assert.Equal(256, genericThumbnail.Width); Assert.Equal(170, genericThumbnail.Height);