/// <summary> /// Function to load an image from a stream. /// </summary> /// <param name="stream">The stream containing the image data to read.</param> /// <param name="size">The size of the image within the stream, in bytes.</param> /// <returns>A <see cref="IGorgonImage"/> containing the image data from the stream.</returns> /// <exception cref="GorgonException">Thrown when the image data in the stream has a pixel format that is unsupported.</exception> protected override IGorgonImage OnDecodeFromStream(Stream stream, long size) { var wic = new WicUtilities(); Stream streamAlias = stream; try { // If we have a stream position that does not begin exactly at the start of the stream, we have to wrap that stream in a // dummy stream wrapper. This is to get around a problem in the underlying COM stream object used by WIC that throws an // exception when the stream position is not exactly 0. if (streamAlias.Position != 0) { streamAlias = new GorgonStreamWrapper(stream, 0, size); } IGorgonImage result = wic.DecodeImageData(streamAlias, size, SupportedFileFormat, DecodingOptions, FrameOffsetMetadataNames); if (result == null) { throw new IOException(string.Format(Resources.GORIMG_ERR_FILE_FORMAT_NOT_CORRECT, Codec)); } if (stream.Position != streamAlias.Position) { stream.Position += streamAlias.Position; } return(result); } finally { if (streamAlias != stream) { streamAlias?.Dispose(); } wic.Dispose(); } }