private void Load(Stream stream) { Contract.Requires(stream != null); try { if (!stream.CanRead) { throw new NotSupportedException("Cannot read from the stream."); } if (!stream.CanSeek) { throw new NotSupportedException("The stream does not support seeking."); } var decoders = Decoders.GetAvailableDecoders(); if (decoders.Count > 0) { int maxHeaderSize = decoders.Max(x => x.HeaderSize); if (maxHeaderSize > 0) { byte[] header = new byte[maxHeaderSize]; stream.Read(header, 0, maxHeaderSize); stream.Position = 0; var decoder = decoders.FirstOrDefault(x => x.IsSupportedFileFormat(header)); if (decoder != null) { decoder.Decode(this, stream); IsLoading = false; } } } if (IsLoading) { IsLoading = false; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine("Image cannot be loaded. Available decoders:"); foreach (IImageDecoder decoder in decoders) { stringBuilder.AppendLine("-" + decoder); } throw new UnsupportedImageFormatException(stringBuilder.ToString()); } } finally { stream.Dispose(); } }
public void Load(Stream stream) { //Contract.Requires(stream != null); try { if (!stream.CanRead) { throw new NotSupportedException("Cannot read from the stream."); } if (!stream.CanSeek) { throw new NotSupportedException("The stream does not support seeking."); } List <IImageDecoder> decoders = Decoders.GetAvailableDecoders(); if (decoders.Count > 0) { int maxHeaderSize = FindMax(decoders); if (maxHeaderSize > 0) { //temp header... byte[] header = new byte[maxHeaderSize]; stream.Read(header, 0, maxHeaderSize); stream.Position = 0; var decoder = FindFirstSupport(decoders, header); //decoders.FirstOrDefault(x => x.IsSupportedFileFormat(header)); if (decoder != null) { decoder.Decode(this, stream); } } } } finally { stream.Dispose(); } }