diff --git a/src/ImageSharp/Image.FromStream.cs b/src/ImageSharp/Image.FromStream.cs index 86baa1c1a..f7ff9966d 100644 --- a/src/ImageSharp/Image.FromStream.cs +++ b/src/ImageSharp/Image.FromStream.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Text; using System.Threading; @@ -393,20 +394,12 @@ namespace SixLabors.ImageSharp format = data.Format; - if (data.Image != null) + if (data.Image is null) { - return data.Image; + ThrowNotLoaded(options); } - var sb = new StringBuilder(); - sb.AppendLine("Image cannot be loaded. Available decoders:"); - - foreach (KeyValuePair val in options.Configuration.ImageFormatsManager.ImageDecoders) - { - sb.AppendFormat(" - {0} : {1}{2}", val.Key.Name, val.Value.GetType().Name, Environment.NewLine); - } - - throw new UnknownImageFormatException(sb.ToString()); + return data.Image; } /// @@ -426,27 +419,16 @@ namespace SixLabors.ImageSharp Stream stream, CancellationToken cancellationToken = default) { - (Image Image, IImageFormat Format) data = await WithSeekableStreamAsync( - options, - stream, - (s, ct) => Decode(options, s, ct), - cancellationToken) - .ConfigureAwait(false); - - if (data.Image != null) - { - return data; - } + (Image Image, IImageFormat Format) data = + await WithSeekableStreamAsync(options, stream, (s, ct) => Decode(options, s, ct), cancellationToken) + .ConfigureAwait(false); - var sb = new StringBuilder(); - sb.AppendLine("Image cannot be loaded. Available decoders:"); - - foreach (KeyValuePair val in options.Configuration.ImageFormatsManager.ImageDecoders) + if (data.Image is null) { - sb.AppendFormat(" - {0} : {1}{2}", val.Key.Name, val.Value.GetType().Name, Environment.NewLine); + ThrowNotLoaded(options); } - throw new UnknownImageFormatException(sb.ToString()); + return data; } /// @@ -469,27 +451,15 @@ namespace SixLabors.ImageSharp where TPixel : unmanaged, IPixel { (Image Image, IImageFormat Format) data = - await WithSeekableStreamAsync( - options, - stream, - (s, ct) => Decode(options, s, ct), - cancellationToken) - .ConfigureAwait(false); - - if (data.Image != null) - { - return data; - } - - var sb = new StringBuilder(); - sb.AppendLine("Image cannot be loaded. Available decoders:"); + await WithSeekableStreamAsync(options, stream, (s, ct) => Decode(options, s, ct), cancellationToken) + .ConfigureAwait(false); - foreach (KeyValuePair val in options.Configuration.ImageFormatsManager.ImageDecoders) + if (data.Image is null) { - sb.AppendFormat(" - {0} : {1}{2}", val.Key.Name, val.Value.GetType().Name, Environment.NewLine); + ThrowNotLoaded(options); } - throw new UnknownImageFormatException(sb.ToString()); + return data; } /// @@ -512,7 +482,7 @@ namespace SixLabors.ImageSharp where TPixel : unmanaged, IPixel { (Image img, _) = await LoadWithFormatAsync(options, stream, cancellationToken) - .ConfigureAwait(false); + .ConfigureAwait(false); return img; } @@ -535,20 +505,12 @@ namespace SixLabors.ImageSharp format = fmt; - if (img != null) + if (img is null) { - return img; + ThrowNotLoaded(options); } - var sb = new StringBuilder(); - sb.AppendLine("Image cannot be loaded. Available decoders:"); - - foreach (KeyValuePair val in options.Configuration.ImageFormatsManager.ImageDecoders) - { - sb.AppendFormat(" - {0} : {1}{2}", val.Key.Name, val.Value.GetType().Name, Environment.NewLine); - } - - throw new UnknownImageFormatException(sb.ToString()); + return img; } /// @@ -633,5 +595,19 @@ namespace SixLabors.ImageSharp return action(memoryStream, cancellationToken); } + + [DoesNotReturn] + private static void ThrowNotLoaded(DecoderOptions options) + { + var sb = new StringBuilder(); + sb.AppendLine("Image cannot be loaded. Available decoders:"); + + foreach (KeyValuePair val in options.Configuration.ImageFormatsManager.ImageDecoders) + { + sb.AppendFormat(" - {0} : {1}{2}", val.Key.Name, val.Value.GetType().Name, Environment.NewLine); + } + + throw new UnknownImageFormatException(sb.ToString()); + } } }