Browse Source

Change GetThumbnail to TryGetThumbnail

pull/2320/head
Stefan Nikolei 4 years ago
parent
commit
a39ac71837
  1. 31
      src/ImageSharp/Metadata/Profiles/Exif/ExifProfile.cs
  2. 7
      tests/ImageSharp.Tests/Metadata/Profiles/Exif/ExifProfileTests.cs

31
src/ImageSharp/Metadata/Profiles/Exif/ExifProfile.cs

@ -124,36 +124,47 @@ public sealed class ExifProfile : IDeepCloneable<ExifProfile>
/// <summary> /// <summary>
/// Returns the thumbnail in the EXIF profile when available. /// Returns the thumbnail in the EXIF profile when available.
/// </summary> /// </summary>
/// <param name="image">The thumbnail</param>
/// <returns> /// <returns>
/// The <see cref="Image"/>. /// True, if there is a thumbnail otherwise false.
/// </returns> /// </returns>
public Image? CreateThumbnail() => this.CreateThumbnail<Rgba32>(); public bool TryCreateThumbnail([NotNullWhen(true)] out Image? image)
{
if (this.TryCreateThumbnail(out Image<Rgba32>? innerimage))
{
image = innerimage;
return true;
}
image = null;
return false;
}
/// <summary> /// <summary>
/// Returns the thumbnail in the EXIF profile when available. /// Returns the thumbnail in the EXIF profile when available.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam> /// <typeparam name="TPixel">The pixel format.</typeparam>
/// <returns> /// <param name="image">The thumbnail.</param>
/// The <see cref="Image{TPixel}"/>. /// <returns>True, if there is a thumbnail otherwise false.</returns>
/// </returns> public bool TryCreateThumbnail<TPixel>([NotNullWhen(true)] out Image<TPixel>? image)
public Image<TPixel>? CreateThumbnail<TPixel>()
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
this.InitializeValues(); this.InitializeValues();
image = null;
if (this.thumbnailOffset == 0 || this.thumbnailLength == 0) if (this.thumbnailOffset == 0 || this.thumbnailLength == 0)
{ {
return null; return false;
} }
if (this.data is null || this.data.Length < (this.thumbnailOffset + this.thumbnailLength)) 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)) using (var memStream = new MemoryStream(this.data, this.thumbnailOffset, this.thumbnailLength))
{ {
return Image.Load<TPixel>(memStream); image = Image.Load<TPixel>(memStream);
return true;
} }
} }

7
tests/ImageSharp.Tests/Metadata/Profiles/Exif/ExifProfileTests.cs

@ -345,13 +345,14 @@ public class ExifProfileTests
ExifProfile profile = GetExifProfile(); ExifProfile profile = GetExifProfile();
TestProfile(profile); TestProfile(profile);
bool retVal = profile.TryCreateThumbnail(out Image thumbnail);
using Image thumbnail = profile.CreateThumbnail(); Assert.True(retVal);
Assert.NotNull(thumbnail); Assert.NotNull(thumbnail);
Assert.Equal(256, thumbnail.Width); Assert.Equal(256, thumbnail.Width);
Assert.Equal(170, thumbnail.Height); Assert.Equal(170, thumbnail.Height);
using Image<Rgba32> genericThumbnail = profile.CreateThumbnail<Rgba32>(); retVal = profile.TryCreateThumbnail<Rgba32>(out Image<Rgba32> genericThumbnail);
Assert.True(retVal);
Assert.NotNull(genericThumbnail); Assert.NotNull(genericThumbnail);
Assert.Equal(256, genericThumbnail.Width); Assert.Equal(256, genericThumbnail.Width);
Assert.Equal(170, genericThumbnail.Height); Assert.Equal(170, genericThumbnail.Height);

Loading…
Cancel
Save