Browse Source

Normalize not loaded exception

pull/2180/head
James Jackson-South 4 years ago
parent
commit
085ff1ae46
  1. 90
      src/ImageSharp/Image.FromStream.cs

90
src/ImageSharp/Image.FromStream.cs

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

Loading…
Cancel
Save