/// <summary> /// Writes to an unmanaged stream. /// </summary> protected unsafe int Write(FreeImageIO io, fi_handle handle, uint size, uint count, ref byte[] buffer) { fixed(byte *ptr = buffer) { return((int)io.writeProc(new IntPtr(ptr), size, count, handle)); } }
/// <summary> /// Reads the requested data from the stream and writes it to the given address. /// </summary> static unsafe uint streamRead(IntPtr buffer, uint size, uint count, fi_handle handle) { Stream stream = handle.GetObject() as Stream; if ((stream == null) || (!stream.CanRead)) { return 0; } uint readCount = 0; byte* ptr = (byte*)buffer; byte[] bufferTemp = new byte[size]; int read; while (readCount < count) { read = stream.Read(bufferTemp, 0, (int)size); if (read != (int)size) { stream.Seek(-read, SeekOrigin.Current); break; } for (int i = 0; i < read; i++, ptr++) { *ptr = bufferTemp[i]; } readCount++; } return (uint)readCount; }
// Saving function protected override bool SaveProc(ref FreeImageIO io, FIBITMAP dib, fi_handle handle, int page, int flags, IntPtr data) { SerialDib sdib; uint size = FreeImage.GetDIBSize(dib); // Store all data needed to recreate the bitmap sdib.width = FreeImage.GetWidth(dib); sdib.height = FreeImage.GetHeight(dib); sdib.pitch = (int)FreeImage.GetPitch(dib); sdib.bpp = FreeImage.GetBPP(dib); sdib.red_mask = FreeImage.GetRedMask(dib); sdib.green_mask = FreeImage.GetGreenMask(dib); sdib.blue_mask = FreeImage.GetBlueMask(dib); sdib.data = new byte[size]; // Copy the bitmaps data into the structures byte-array // The marshaller is used to create an IntPtr for using // 'ConvertToRawBits'. FreeImage.ConvertToRawBits(Marshal.UnsafeAddrOfPinnedArrayElement(sdib.data, 0), dib, sdib.pitch, sdib.bpp, sdib.red_mask, sdib.green_mask, sdib.blue_mask, false); // Use the healper function to write the header to the destination if (Write(io, handle, (uint)header.Length, 1, ref header) != 1) { return(false); } // Create a serializer BinaryFormatter formatter = new BinaryFormatter(); // Create a temporary stream MemoryStream stream = new MemoryStream(); // Create a compression stream GZipStream zipStream = new GZipStream(stream, CompressionMode.Compress); // Serialize the structure into the compression stream formatter.Serialize(zipStream, sdib); // Unload the compression stream zipStream.Dispose(); // Get the result data byte[] buffer = stream.GetBuffer(); // Use the healper function 'Write' to write the data to the destination if (Write(io, handle, 1, (uint)buffer.Length, ref buffer) != buffer.Length) { // Unload the temporary stream stream.Dispose(); return(false); } // Unload the temporary stream stream.Dispose(); return(true); }
// Loading function protected override FIBITMAP LoadProc(ref FreeImageIO io, fi_handle handle, int page, int flags, IntPtr data) { // Check if the data has the correct format if (!ValidateProc(ref io, handle)) { // Create a free-image message FreeImage.OutputMessageProc(format, "Invalid format."); // return 0 (operation failed) return(FIBITMAP.Zero); } SerialDib sdib; int read = 0; System.IO.MemoryStream stream = new System.IO.MemoryStream(); byte[] buffer = new byte[1024]; do { // Use the helper function 'Read' to read from the source read = Read(io, handle, 1, 1024, ref buffer); // Store the data in a temporary buffer stream.Write(buffer, 0, read); }while (read != 0); // Set the memory stream back to the beginning. stream.Position = 0; // Unzip the stream GZipStream zipStream = new GZipStream(stream, CompressionMode.Decompress); // Create a serializer BinaryFormatter formatter = new BinaryFormatter(); // Deserialize the stream sdib = (SerialDib)formatter.Deserialize(zipStream); // Unload the stream zipStream.Dispose(); // Use 'ConvertFromRawBits and the deserialized struct to recreate the bitmap // In this case the marshaller is used to create the needed IntPtr to the data // array. FIBITMAP dib = FreeImage.ConvertFromRawBits( Marshal.UnsafeAddrOfPinnedArrayElement(sdib.data, 0), (int)sdib.width, (int)sdib.height, sdib.pitch, sdib.bpp, sdib.red_mask, sdib.green_mask, sdib.blue_mask, false); // Unload the temporary stream stream.Dispose(); // Return the created bitmap return(dib); }
// Loading function protected override FIBITMAP LoadProc(ref FreeImageIO io, fi_handle handle, int page, int flags, IntPtr data) { // Check if the data has the correct format if (!ValidateProc(ref io, handle)) { // Create a free-image message FreeImage.OutputMessageProc(format, "Invalid format."); // return 0 (operation failed) return FIBITMAP.Zero; } SerialDib sdib; int read = 0; System.IO.MemoryStream stream = new System.IO.MemoryStream(); byte[] buffer = new byte[1024]; do { // Use the helper function 'Read' to read from the source read = Read(io, handle, 1, 1024, ref buffer); // Store the data in a temporary buffer stream.Write(buffer, 0, read); } while (read != 0); // Set the memory stream back to the beginning. stream.Position = 0; // Unzip the stream GZipStream zipStream = new GZipStream(stream, CompressionMode.Decompress); // Create a serializer BinaryFormatter formatter = new BinaryFormatter(); // Deserialize the stream sdib = (SerialDib)formatter.Deserialize(zipStream); // Unload the stream zipStream.Dispose(); // Use 'ConvertFromRawBits and the deserialized struct to recreate the bitmap // In this case the marshaller is used to create the needed IntPtr to the data // array. FIBITMAP dib = FreeImage.ConvertFromRawBits( Marshal.UnsafeAddrOfPinnedArrayElement(sdib.data, 0), (int)sdib.width, (int)sdib.height, sdib.pitch, sdib.bpp, sdib.red_mask, sdib.green_mask, sdib.blue_mask, false); // Unload the temporary stream stream.Dispose(); // Return the created bitmap return dib; }
// The function reads the first bytes of the file and compares it // with the predefined header. protected override bool ValidateProc(ref FreeImageIO io, fi_handle handle) { for (int i = 0; i < header.Length; i++) { if (ReadByte(io, handle) != header[i]) { return(false); } } return(true); }
public void fi_handle() { fi_handle var = new fi_handle(); Assert.IsTrue(var.IsNull); string test = "hello word!"; using (var = new fi_handle(test)) { Assert.IsFalse(var.IsNull); object obj = var.GetObject(); Assert.That(obj is string); Assert.AreSame(obj, test); } }
/// <summary> /// Function that can be implemented. /// </summary> protected virtual IntPtr OpenProc(ref FreeImageIO io, fi_handle handle, bool read) { return(IntPtr.Zero); }
public static extern FIMULTIBITMAP OpenMultiBitmapFromHandle(FREE_IMAGE_FORMAT fif, ref FreeImageIO io, fi_handle handle, FREE_IMAGE_LOAD_FLAGS flags);
public static extern FREE_IMAGE_FORMAT GetFileTypeFromHandle(ref FreeImageIO io, fi_handle handle, int size);
/// <summary> /// Seeks in an unmanaged stream. /// </summary> protected int Seek(FreeImageIO io, fi_handle handle, int offset, SeekOrigin origin) { return(io.seekProc(handle, offset, origin)); }
public void FreeImageStreamIO() { Random rand = new Random(); byte[] bBuffer = new byte[256]; IntPtr buffer = Marshal.AllocHGlobal(256); MemoryStream stream = new MemoryStream(); Assert.IsNotNull(stream); using (fi_handle handle = new fi_handle(stream)) { FreeImageIO io = FreeImageAPI.IO.FreeImageStreamIO.io; Assert.IsNotNull(io.readProc); Assert.IsNotNull(io.writeProc); Assert.IsNotNull(io.seekProc); Assert.IsNotNull(io.tellProc); // // Procs // rand.NextBytes(bBuffer); stream.Write(bBuffer, 0, bBuffer.Length); Assert.That(io.tellProc(handle) == stream.Position); Assert.That(io.seekProc(handle, 0, SeekOrigin.Begin) == 0); Assert.That(io.tellProc(handle) == 0); Assert.That(io.tellProc(handle) == stream.Position); // Read one block Assert.That(io.readProc(buffer, (uint)bBuffer.Length, 1, handle) == 1); for (int i = 0; i < bBuffer.Length; i++) Assert.That(Marshal.ReadByte(buffer, i) == bBuffer[i]); Assert.That(io.tellProc(handle) == stream.Position); Assert.That(io.seekProc(handle, 0, SeekOrigin.Begin) == 0); Assert.That(io.tellProc(handle) == stream.Position); // Read 1 byte block Assert.That(io.readProc(buffer, 1, (uint)bBuffer.Length, handle) == bBuffer.Length); for (int i = 0; i < bBuffer.Length; i++) Assert.That(Marshal.ReadByte(buffer, i) == bBuffer[i]); Assert.That(io.tellProc(handle) == stream.Position); Assert.That(io.seekProc(handle, 0, SeekOrigin.Begin) == 0); Assert.That(io.tellProc(handle) == stream.Position); rand.NextBytes(bBuffer); for (int i = 0; i < bBuffer.Length; i++) Marshal.WriteByte(buffer, i, bBuffer[i]); // Write one block Assert.That(io.writeProc(buffer, (uint)bBuffer.Length, 1, handle) == 1); for (int i = 0; i < bBuffer.Length; i++) Assert.That(Marshal.ReadByte(buffer, i) == bBuffer[i]); Assert.That(io.tellProc(handle) == stream.Position); Assert.That(io.seekProc(handle, 0, SeekOrigin.Begin) == 0); Assert.That(io.tellProc(handle) == 0); // write 1 byte block Assert.That(io.writeProc(buffer, 1, (uint)bBuffer.Length, handle) == bBuffer.Length); for (int i = 0; i < bBuffer.Length; i++) Assert.That(Marshal.ReadByte(buffer, i) == bBuffer[i]); Assert.That(io.tellProc(handle) == stream.Position); // Seek and tell Assert.That(io.seekProc(handle, 0, SeekOrigin.Begin) == 0); Assert.That(io.tellProc(handle) == 0); Assert.That(io.seekProc(handle, 127, SeekOrigin.Current) == 0); Assert.That(io.tellProc(handle) == 127); Assert.That(io.seekProc(handle, 0, SeekOrigin.End) == 0); Assert.That(io.tellProc(handle) == 256); Marshal.FreeHGlobal(buffer); stream.Dispose(); } }
/// <summary> /// Saves a previously loaded FreeImage bitmap to a stream. /// The stream must be set to the correct position before calling SaveToStream. /// </summary> /// <param name="dib">Handle to a FreeImage bitmap.</param> /// <param name="stream">The stream to write to.</param> /// <param name="format">Format of the image.</param> /// <param name="flags">Flags to enable or disable plugin-features.</param> /// <param name="colorDepth">The new color depth of the bitmap. /// Set to <see cref="FREE_IMAGE_COLOR_DEPTH.FICD_AUTO"/> if SaveToStream should /// take the best suitable color depth. /// If a color depth is selected that the provided format cannot write an /// error-message will be thrown.</param> /// <param name="unloadSource">When true the structure will be unloaded on success.</param> /// <returns>Returns true on success, false on failure.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="dib"/> or <paramref name="stream"/> is null.</exception> /// <exception cref="ArgumentException"> /// <paramref name="stream"/> cannot write.</exception> public static bool SaveToStream( ref FIBITMAP dib, Stream stream, FREE_IMAGE_FORMAT format, FREE_IMAGE_SAVE_FLAGS flags, FREE_IMAGE_COLOR_DEPTH colorDepth, bool unloadSource) { if (dib.IsNull) { throw new ArgumentNullException("dib"); } if (stream == null) { throw new ArgumentNullException("stream"); } if (!stream.CanWrite) { throw new ArgumentException("stream is not capable of writing."); } if ((!FIFSupportsWriting(format)) || (!FIFSupportsExportType(format, FREE_IMAGE_TYPE.FIT_BITMAP))) { return false; } FIBITMAP dibToSave = PrepareBitmapColorDepth(dib, format, colorDepth); bool result = false; try { // Create a 'FreeImageIO' structure for calling 'SaveToHandle' FreeImageIO io = FreeImageStreamIO.io; using (fi_handle handle = new fi_handle(stream)) { result = SaveToHandle(format, dibToSave, ref io, handle, flags); } } catch { // Always unload a temporary created bitmap. if (dibToSave != dib) { UnloadEx(ref dibToSave); } // On success unload the bitmap if (result && unloadSource) { UnloadEx(ref dib); } } return result; }
/// <summary> /// Function that can be implemented. /// </summary> protected virtual bool SaveProc(ref FreeImageIO io, FIBITMAP dib, fi_handle handle, int page, int flags, IntPtr data) { return false; }
/// <summary> /// Reads a single byte from an unmanaged stream. /// </summary> protected unsafe int ReadByte(FreeImageIO io, fi_handle handle) { byte buffer = 0; return (int)io.readProc(new IntPtr(&buffer), 1, 1, handle) > 0 ? buffer : -1; }
/// <summary> /// Function that can be implemented. /// </summary> protected virtual int PageCountProc(ref FreeImageIO io, fi_handle handle, IntPtr data) { return 0; }
/// <summary> /// Function that can be implemented. /// </summary> protected virtual IntPtr OpenProc(ref FreeImageIO io, fi_handle handle, bool read) { return IntPtr.Zero; }
/// <summary> /// Function that can be implemented. /// </summary> protected virtual FIBITMAP LoadProc(ref FreeImageIO io, fi_handle handle, int page, int flags, IntPtr data) { return FIBITMAP.Zero; }
/// <summary> /// Function that can be implemented. /// </summary> protected virtual void CloseProc(ref FreeImageIO io, fi_handle handle, IntPtr data) { }
/// <summary> /// Function that can be implemented. /// </summary> protected virtual int PageCapabilityProc(ref FreeImageIO io, fi_handle handle, IntPtr data) { return(0); }
/// <summary> /// Function that can be implemented. /// </summary> protected virtual bool SaveProc(ref FreeImageIO io, FIBITMAP dib, fi_handle handle, int page, int flags, IntPtr data) { return(false); }
// Saving function protected override bool SaveProc(ref FreeImageIO io, FIBITMAP dib, fi_handle handle, int page, int flags, IntPtr data) { SerialDib sdib; uint size = FreeImage.GetDIBSize(dib); // Store all data needed to recreate the bitmap sdib.width = FreeImage.GetWidth(dib); sdib.height = FreeImage.GetHeight(dib); sdib.pitch = (int)FreeImage.GetPitch(dib); sdib.bpp = FreeImage.GetBPP(dib); sdib.red_mask = FreeImage.GetRedMask(dib); sdib.green_mask = FreeImage.GetGreenMask(dib); sdib.blue_mask = FreeImage.GetBlueMask(dib); sdib.data = new byte[size]; // Copy the bitmaps data into the structures byte-array // The marshaller is used to create an IntPtr for using // 'ConvertToRawBits'. FreeImage.ConvertToRawBits(Marshal.UnsafeAddrOfPinnedArrayElement(sdib.data, 0), dib, sdib.pitch, sdib.bpp, sdib.red_mask, sdib.green_mask, sdib.blue_mask, false); // Use the healper function to write the header to the destination if (Write(io, handle, (uint)header.Length, 1, ref header) != 1) return false; // Create a serializer BinaryFormatter formatter = new BinaryFormatter(); // Create a temporary stream MemoryStream stream = new MemoryStream(); // Create a compression stream GZipStream zipStream = new GZipStream(stream, CompressionMode.Compress); // Serialize the structure into the compression stream formatter.Serialize(zipStream, sdib); // Unload the compression stream zipStream.Dispose(); // Get the result data byte[] buffer = stream.GetBuffer(); // Use the healper function 'Write' to write the data to the destination if (Write(io, handle, 1, (uint)buffer.Length, ref buffer) != buffer.Length) { // Unload the temporary stream stream.Dispose(); return false; } // Unload the temporary stream stream.Dispose(); return true; }
/// <summary> /// Orders FreeImage to analyze the bitmap signature. /// In case the stream is not seekable, the stream will have been used /// and must be recreated for loading. /// </summary> /// <param name="stream">Name of the stream to analyze.</param> /// <returns>Type of the bitmap.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="stream"/> is null.</exception> /// <exception cref="ArgumentException"> /// <paramref name="stream"/> can not read.</exception> public static FREE_IMAGE_FORMAT GetFileTypeFromStream(Stream stream) { if (stream == null) { throw new ArgumentNullException("stream"); } if (!stream.CanRead) { throw new ArgumentException("stream is not capable of reading."); } // Wrap the stream if it cannot seek stream = (stream.CanSeek) ? stream : new StreamWrapper(stream, true); // Create a 'FreeImageIO' structure for the stream FreeImageIO io = FreeImageStreamIO.io; using (fi_handle handle = new fi_handle(stream)) { return GetFileTypeFromHandle(ref io, handle, 0); } }
public void FreeImage_LoadFromHandle() { byte[] data = File.ReadAllBytes(iManager.GetBitmapPath(ImageType.Even, ImageColorType.Type_16_555)); MemoryStream mStream = new MemoryStream(data); FreeImageIO io = FreeImageStreamIO.io; using (fi_handle handle = new fi_handle(mStream)) { dib = FreeImage.LoadFromHandle(FREE_IMAGE_FORMAT.FIF_BMP, ref io, handle, FREE_IMAGE_LOAD_FLAGS.DEFAULT); Assert.That(!dib.IsNull); FreeImage.UnloadEx(ref dib); } }
/// <summary> /// Seeks in an unmanaged stream. /// </summary> protected int Seek(FreeImageIO io, fi_handle handle, int offset, SeekOrigin origin) { return io.seekProc(handle, offset, origin); }
// The function reads the first bytes of the file and compares it // with the predefined header. protected override bool ValidateProc(ref FreeImageIO io, fi_handle handle) { for (int i = 0; i < header.Length; i++) if (ReadByte(io, handle) != header[i]) return false; return true; }
/// <summary> /// Retrieves the position of an unmanaged stream. /// </summary> protected int Tell(FreeImageIO io, fi_handle handle) { return io.tellProc(handle); }
/// <summary> /// Reads a single byte from an unmanaged stream. /// </summary> protected unsafe int ReadByte(FreeImageIO io, fi_handle handle) { byte buffer = 0; return((int)io.readProc(new IntPtr(&buffer), 1, 1, handle) > 0 ? buffer : -1); }
/// <summary> /// Function that can be implemented. /// </summary> protected virtual bool ValidateProc(ref FreeImageIO io, fi_handle handle) { return false; }
/// <summary> /// Writes a single byte to an unmanaged stream. /// </summary> protected unsafe int WriteByte(FreeImageIO io, fi_handle handle, byte value) { return((int)io.writeProc(new IntPtr(&value), 1, 1, handle)); }
/// <summary> /// Writes to an unmanaged stream. /// </summary> protected unsafe int Write(FreeImageIO io, fi_handle handle, uint size, uint count, ref byte[] buffer) { fixed (byte* ptr = buffer) { return (int)io.writeProc(new IntPtr(ptr), size, count, handle); } }
/// <summary> /// Retrieves the position of an unmanaged stream. /// </summary> protected int Tell(FreeImageIO io, fi_handle handle) { return(io.tellProc(handle)); }
/// <summary> /// Writes a single byte to an unmanaged stream. /// </summary> protected unsafe int WriteByte(FreeImageIO io, fi_handle handle, byte value) { return (int)io.writeProc(new IntPtr(&value), 1, 1, handle); }
public static extern FIBITMAP LoadFromHandle(FREE_IMAGE_FORMAT fif, ref FreeImageIO io, fi_handle handle, FREE_IMAGE_LOAD_FLAGS flags);
/// <summary> /// Moves the streams position. /// </summary> static int streamSeek(fi_handle handle, int offset, SeekOrigin origin) { Stream stream = handle.GetObject() as Stream; if (stream == null) { return 1; } stream.Seek((long)offset, origin); return 0; }
public static extern bool SaveToHandle(FREE_IMAGE_FORMAT fif, FIBITMAP dib, ref FreeImageIO io, fi_handle handle, FREE_IMAGE_SAVE_FLAGS flags);
/// <summary> /// Returns the streams current position /// </summary> static int streamTell(fi_handle handle) { Stream stream = handle.GetObject() as Stream; if (stream == null) { return -1; } return (int)stream.Position; }
/// <summary> /// Reads the given data and writes it into the stream. /// </summary> static unsafe uint streamWrite(IntPtr buffer, uint size, uint count, fi_handle handle) { Stream stream = handle.GetObject() as Stream; if ((stream == null) || (!stream.CanWrite)) { return 0; } uint writeCount = 0; byte[] bufferTemp = new byte[size]; byte* ptr = (byte*)buffer; while (writeCount < count) { for (int i = 0; i < size; i++, ptr++) { bufferTemp[i] = *ptr; } try { stream.Write(bufferTemp, 0, bufferTemp.Length); } catch { return writeCount; } writeCount++; } return writeCount; }
/// <summary> /// Function that can be implemented. /// </summary> protected virtual FIBITMAP LoadProc(ref FreeImageIO io, fi_handle handle, int page, int flags, IntPtr data) { return(FIBITMAP.Zero); }
public void FreeImageStreamIO() { Random rand = new Random(); byte[] bBuffer = new byte[256]; IntPtr buffer = Marshal.AllocHGlobal(256); MemoryStream stream = new MemoryStream(); Assert.IsNotNull(stream); using (fi_handle handle = new fi_handle(stream)) { FreeImageIO io = FreeImageAPI.IO.FreeImageStreamIO.io; Assert.IsNotNull(io.readProc); Assert.IsNotNull(io.writeProc); Assert.IsNotNull(io.seekProc); Assert.IsNotNull(io.tellProc); // // Procs // rand.NextBytes(bBuffer); stream.Write(bBuffer, 0, bBuffer.Length); Assert.That(io.tellProc(handle) == stream.Position); Assert.That(io.seekProc(handle, 0, SeekOrigin.Begin) == 0); Assert.That(io.tellProc(handle) == 0); Assert.That(io.tellProc(handle) == stream.Position); // Read one block Assert.That(io.readProc(buffer, (uint)bBuffer.Length, 1, handle) == 1); for (int i = 0; i < bBuffer.Length; i++) { Assert.That(Marshal.ReadByte(buffer, i) == bBuffer[i]); } Assert.That(io.tellProc(handle) == stream.Position); Assert.That(io.seekProc(handle, 0, SeekOrigin.Begin) == 0); Assert.That(io.tellProc(handle) == stream.Position); // Read 1 byte block Assert.That(io.readProc(buffer, 1, (uint)bBuffer.Length, handle) == bBuffer.Length); for (int i = 0; i < bBuffer.Length; i++) { Assert.That(Marshal.ReadByte(buffer, i) == bBuffer[i]); } Assert.That(io.tellProc(handle) == stream.Position); Assert.That(io.seekProc(handle, 0, SeekOrigin.Begin) == 0); Assert.That(io.tellProc(handle) == stream.Position); rand.NextBytes(bBuffer); for (int i = 0; i < bBuffer.Length; i++) { Marshal.WriteByte(buffer, i, bBuffer[i]); } // Write one block Assert.That(io.writeProc(buffer, (uint)bBuffer.Length, 1, handle) == 1); for (int i = 0; i < bBuffer.Length; i++) { Assert.That(Marshal.ReadByte(buffer, i) == bBuffer[i]); } Assert.That(io.tellProc(handle) == stream.Position); Assert.That(io.seekProc(handle, 0, SeekOrigin.Begin) == 0); Assert.That(io.tellProc(handle) == 0); // write 1 byte block Assert.That(io.writeProc(buffer, 1, (uint)bBuffer.Length, handle) == bBuffer.Length); for (int i = 0; i < bBuffer.Length; i++) { Assert.That(Marshal.ReadByte(buffer, i) == bBuffer[i]); } Assert.That(io.tellProc(handle) == stream.Position); // Seek and tell Assert.That(io.seekProc(handle, 0, SeekOrigin.Begin) == 0); Assert.That(io.tellProc(handle) == 0); Assert.That(io.seekProc(handle, 127, SeekOrigin.Current) == 0); Assert.That(io.tellProc(handle) == 127); Assert.That(io.seekProc(handle, 0, SeekOrigin.End) == 0); Assert.That(io.tellProc(handle) == 256); Marshal.FreeHGlobal(buffer); stream.Dispose(); } }
/// <summary> /// Function that can be implemented. /// </summary> protected virtual bool ValidateProc(ref FreeImageIO io, fi_handle handle) { return(false); }
/// <summary> /// Loads a FreeImage bitmap. /// In case the loading format is <see cref="FREE_IMAGE_FORMAT.FIF_UNKNOWN"/> /// the bitmaps real format is being analysed. /// The stream must be set to the correct position before calling LoadFromStream. /// </summary> /// <param name="stream">The stream to read from.</param> /// <param name="flags">Flags to enable or disable plugin-features.</param> /// <param name="format">Format of the image. If the format is unknown use /// <see cref="FREE_IMAGE_FORMAT.FIF_UNKNOWN"/>. /// In case a suitable format was found by LoadFromStream it will be returned in format.</param> /// <returns>Handle to a FreeImage bitmap.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="stream"/> is null.</exception> /// <exception cref="ArgumentException"> /// <paramref name="stream"/> is not capable of reading.</exception> public static FIBITMAP LoadFromStream( Stream stream, FREE_IMAGE_LOAD_FLAGS flags, ref FREE_IMAGE_FORMAT format) { if (stream == null) { throw new ArgumentNullException("stream"); } if (!stream.CanRead) { throw new ArgumentException("stream is not capable of reading."); } // Wrap the source stream if it is unable to seek (which is required by FreeImage) stream = (stream.CanSeek) ? stream : new StreamWrapper(stream, true); // Save the streams position if (format == FREE_IMAGE_FORMAT.FIF_UNKNOWN) { long position = stream.Position; // Get the format of the bitmap format = GetFileTypeFromStream(stream); // Restore the streams position stream.Position = position; } if (!FIFSupportsReading(format)) { return 0; } // Create a 'FreeImageIO' structure for calling 'LoadFromHandle' // using the internal structure 'FreeImageStreamIO'. FreeImageIO io = FreeImageStreamIO.io; using (fi_handle handle = new fi_handle(stream)) { return LoadFromHandle(format, ref io, handle, flags); } }