/// <summary> /// Initialise a new FreeImage class from a System.Drawing.Bitmap /// </summary> /// <param name="bitmap">The bitmap.</param> /// <param name="imageFormat">The image format.</param> /// <exception cref="Exception">Image format \"" + imageFormat.ToString() + "\" is not supported</exception> public FreeImage(Bitmap bitmap, ImageFormat imageFormat) { Internal.FREE_IMAGE_FORMAT fif = ImageFormatToFIF(imageFormat); if (fif == Internal.FREE_IMAGE_FORMAT.FIF_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 = (IntPtr)Internal.FreeImageApi.OpenMemory(dataPtr, buffer.Length); m_Handle = Internal.FreeImageApi.LoadFromMemory(fif, (uint)m_MemPtr, 0); }
/// <summary> /// Initialise a new FreeImage class from a file /// </summary> /// <param name="filename">The image filename</param> /// <exception cref="Exception">Unknown file format</exception> public FreeImage(string filename) { Internal.FREE_IMAGE_FORMAT fif = Internal.FreeImageApi.GetFIFFromFilename(filename); if (fif == Internal.FREE_IMAGE_FORMAT.FIF_UNKNOWN) { throw new Exception("Unknown file format"); } m_Handle = Internal.FreeImageApi.Load(fif, filename, 0); m_MemPtr = IntPtr.Zero; }
/// <summary> /// Save the image to the specified filename, /// this method retrevie the destination image /// format automatically by checking the file extension /// </summary> /// <param name="filename">The filename.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> public bool Save(string filename) { if (File.Exists(filename)) { File.Delete(filename); } Internal.FREE_IMAGE_FORMAT fif = Internal.FreeImageApi.GetFIFFromFilename(filename); Internal.FreeImageApi.SetPluginEnabled(fif, true); return(Internal.FreeImageApi.Save(fif, m_Handle, filename, 0)); }