From e467d5ca6585f5f2ae8739c8fee6ca1706ae12ca Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 1 Feb 2016 20:52:09 +1100 Subject: [PATCH] Image should not dispose of stream. Fix #316 Former-commit-id: 7818c6866549f63bd8964f1c6650b5deb938f25c Former-commit-id: dfa8dfabf96353a6cde86386d87fd57360eb1df0 Former-commit-id: 3739eafa2260dc0e6ab9f292c7905a5f04e6765e --- src/ImageProcessorCore/Image.cs | 63 +++++++++++++++------------------ 1 file changed, 28 insertions(+), 35 deletions(-) diff --git a/src/ImageProcessorCore/Image.cs b/src/ImageProcessorCore/Image.cs index cfae7cdd0..44c597ed3 100644 --- a/src/ImageProcessorCore/Image.cs +++ b/src/ImageProcessorCore/Image.cs @@ -230,52 +230,45 @@ namespace ImageProcessorCore /// private void Load(Stream stream, IList 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()); } } }