public FreeImage(Bitmap bitmap, ImageFormat imageFormat) { FreeImageFormat fif = ImageFormatToFIF(imageFormat); if (fif == FreeImageFormat.Unknown) { throw new Exception("Image format \"" + imageFormat.ToString() + "\" is not supported"); } MemoryStream ms = new MemoryStream(); bitmap.Save(ms, imageFormat); ms.Flush(); byte[] buffer = new byte[((int)(ms.Length - 1)) + 1]; ms.Position = 0; ms.Read(buffer, 0, (int)ms.Length); ms.Close(); IntPtr dataPtr = Marshal.AllocHGlobal(buffer.Length); Marshal.Copy(buffer, 0, dataPtr, buffer.Length); m_MemPtr = FreeImageApi.OpenMemory(dataPtr, buffer.Length); m_Handle = FreeImageApi.LoadFromMemory(fif, m_MemPtr, 0); }