Browse Source

Image should not dispose of stream. Fix #316

Former-commit-id: 7818c6866549f63bd8964f1c6650b5deb938f25c
Former-commit-id: dfa8dfabf96353a6cde86386d87fd57360eb1df0
Former-commit-id: 3739eafa2260dc0e6ab9f292c7905a5f04e6765e
pull/1/head
James Jackson-South 10 years ago
parent
commit
f5e8014d3b
  1. 63
      src/ImageProcessorCore/Image.cs

63
src/ImageProcessorCore/Image.cs

@ -230,52 +230,45 @@ namespace ImageProcessorCore
/// </exception>
private void Load(Stream stream, IList<IImageFormat> formats)
{
try
if (!stream.CanRead)
{
if (!stream.CanRead)
{
throw new NotSupportedException("Cannot read from the stream.");
}
throw new NotSupportedException("Cannot read from the stream.");
}
if (!stream.CanSeek)
{
throw new NotSupportedException("The stream does not support seeking.");
}
if (!stream.CanSeek)
{
throw new NotSupportedException("The stream does not support seeking.");
}
if (formats.Count > 0)
if (formats.Count > 0)
{
int maxHeaderSize = formats.Max(x => x.Decoder.HeaderSize);
if (maxHeaderSize > 0)
{
int maxHeaderSize = formats.Max(x => x.Decoder.HeaderSize);
if (maxHeaderSize > 0)
byte[] header = new byte[maxHeaderSize];
stream.Read(header, 0, maxHeaderSize);
stream.Position = 0;
IImageFormat format = formats.FirstOrDefault(x => x.Decoder.IsSupportedFileFormat(header));
if (format != null)
{
byte[] header = new byte[maxHeaderSize];
stream.Read(header, 0, maxHeaderSize);
stream.Position = 0;
IImageFormat format = formats.FirstOrDefault(x => x.Decoder.IsSupportedFileFormat(header));
if (format != null)
{
format.Decoder.Decode(this, stream);
this.CurrentImageFormat = format;
return;
}
format.Decoder.Decode(this, stream);
this.CurrentImageFormat = format;
return;
}
}
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("Image cannot be loaded. Available formats:");
foreach (IImageFormat format in formats)
{
stringBuilder.AppendLine("-" + format);
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("Image cannot be loaded. Available formats:");
throw new NotSupportedException(stringBuilder.ToString());
}
finally
foreach (IImageFormat format in formats)
{
stream.Dispose();
stringBuilder.AppendLine("-" + format);
}
throw new NotSupportedException(stringBuilder.ToString());
}
}
}

Loading…
Cancel
Save