Browse Source

Change GetThumbnail to TryGetThumbnail

pull/2320/head
Stefan Nikolei 3 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>
/// Returns the thumbnail in the EXIF profile when available.
/// </summary>
/// <param name="image">The thumbnail</param>
/// <returns>
/// The <see cref="Image"/>.
/// True, if there is a thumbnail otherwise false.
/// </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>
/// Returns the thumbnail in the EXIF profile when available.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <returns>
/// The <see cref="Image{TPixel}"/>.
/// </returns>
public Image<TPixel>? CreateThumbnail<TPixel>()
/// <param name="image">The thumbnail.</param>
/// <returns>True, if there is a thumbnail otherwise false.</returns>
public bool TryCreateThumbnail<TPixel>([NotNullWhen(true)] out Image<TPixel>? image)
where TPixel : unmanaged, IPixel<TPixel>
{
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<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();
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<Rgba32> genericThumbnail = profile.CreateThumbnail<Rgba32>();
retVal = profile.TryCreateThumbnail<Rgba32>(out Image<Rgba32> genericThumbnail);
Assert.True(retVal);
Assert.NotNull(genericThumbnail);
Assert.Equal(256, genericThumbnail.Width);
Assert.Equal(170, genericThumbnail.Height);

Loading…
Cancel
Save