📄 freeimagebitmap.cs
字号:
// ==========================================================
// FreeImage 3 .NET wrapper
// Original FreeImage 3 functions and .NET compatible derived functions
//
// Design and implementation by
// - Jean-Philippe Goerke (jpgoerke@users.sourceforge.net)
// - Carsten Klein (cklein05@users.sourceforge.net)
//
// Contributors:
// - David Boland (davidboland@vodafone.ie)
//
// Main reference : MSDN Knowlede Base
//
// This file is part of FreeImage 3
//
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
// THIS DISCLAIMER.
//
// Use at your own risk!
// ==========================================================
// ==========================================================
// CVS
// $Revision: 1.5 $
// $Date: 2008/07/01 08:32:12 $
// $Id: FreeImageBitmap.cs,v 1.5 2008/07/01 08:32:12 cklein05 Exp $
// ==========================================================
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.IO.Compression;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Collections;
using System.Collections.Generic;
using FreeImageAPI.Metadata;
namespace FreeImageAPI
{
/// <summary>
/// Encapsulates a FreeImage-bitmap.
/// </summary>
[Serializable, Guid("64a4c935-b757-499c-ab8c-6110316a9e51")]
public class FreeImageBitmap : ICloneable, IDisposable, IEnumerable, ISerializable
{
#region Fields
private bool disposed = false;
private object tag = null;
private object lockObject = new object();
private SaveInformation saveInformation = new SaveInformation();
/// <summary>
/// Format of the sourceimage.
/// </summary>
private FREE_IMAGE_FORMAT originalFormat = FREE_IMAGE_FORMAT.FIF_UNKNOWN;
/// <summary>
/// Handle to the encapsulated FreeImage-bitmap.
/// </summary>
private FIBITMAP dib = 0;
/// <summary>
/// Handle to the encapsulated FreeImage-multipagebitmap.
/// </summary>
private FIMULTIBITMAP mdib = 0;
#endregion
#region Constructors and Destructor
/// <summary>
/// Initializes a new instance of the <see cref="FreeImageBitmap"/> class.
/// </summary>
protected FreeImageBitmap()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FreeImageBitmap"/> class.
/// For internal use only.
/// </summary>
/// <exception cref="Exception">The operation failed.</exception>
internal protected FreeImageBitmap(FIBITMAP dib)
{
if (dib.IsNull)
{
throw new Exception();
}
this.dib = dib;
}
/// <summary>
/// Initializes a new instance of the <see cref="FreeImageBitmap"/> class
/// bases on the specified image.
/// </summary>
/// <param name="original">The original to clone from.</param>
/// <exception cref="Exception">The operation failed.</exception>
public FreeImageBitmap(FreeImageBitmap original)
{
original.EnsureNotDisposed();
dib = FreeImage.Clone(original.dib);
if (dib.IsNull)
{
throw new Exception();
}
}
/// <summary>
/// Initializes a new instance of the <see cref="FreeImageBitmap"/> class
/// bases on the specified image with the specified size.
/// </summary>
/// <param name="original">The original to clone from.</param>
/// <param name="newSize">The Size structure that represent the
/// size of the new <see cref="FreeImageBitmap"/>.</param>
/// <exception cref="Exception">The operation failed.</exception>
/// <exception cref="ArgumentNullException"><paramref name="original"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="newSize.Width"/> or <paramref name="newSize.Height"/> are less or equal zero.
/// </exception>
public FreeImageBitmap(FreeImageBitmap original, Size newSize)
: this(original, newSize.Width, newSize.Height)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FreeImageBitmap"/> class
/// bases on the specified image with the specified size.
/// </summary>
/// <param name="original">The original to clone from.</param>
/// <param name="width">Width of the new <see cref="FreeImageBitmap"/>.</param>
/// <param name="height">Height of the new <see cref="FreeImageBitmap"/>.</param>
/// <exception cref="Exception">The operation failed.</exception>
/// <exception cref="ArgumentNullException"><paramref name="original"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="width"/> or <paramref name="height"/> are less or equal zero.</exception>
public FreeImageBitmap(FreeImageBitmap original, int width, int height)
{
if (original == null)
{
throw new ArgumentNullException("original");
}
if (width <= 0)
{
throw new ArgumentOutOfRangeException("width");
}
if (height <= 0)
{
throw new ArgumentOutOfRangeException("height");
}
original.EnsureNotDisposed();
dib = FreeImage.Rescale(original.dib, width, height, FREE_IMAGE_FILTER.FILTER_BICUBIC);
if (dib.IsNull)
{
throw new Exception();
}
}
/// <summary>
/// Initializes a new instance of the <see cref="FreeImageBitmap"/> class
/// bases on the specified image.
/// </summary>
/// <param name="original">The original to clone from.</param>
/// <exception cref="Exception">The operation failed.</exception>
public FreeImageBitmap(Image original)
: this(original as Bitmap)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FreeImageBitmap"/> class
/// bases on the specified image with the specified size.
/// </summary>
/// <param name="original">The original to clone from.</param>
/// <param name="newSize">The Size structure that represent the
/// size of the new <see cref="FreeImageBitmap"/>.</param>
/// <exception cref="Exception">The operation failed.</exception>
/// <exception cref="ArgumentNullException"><paramref name="original"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="newSize.Width"/> or <paramref name="newSize.Height"/> are less or equal zero.
/// </exception>
public FreeImageBitmap(Image original, Size newSize)
: this(original as Bitmap, newSize.Width, newSize.Height)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FreeImageBitmap"/> class
/// bases on the specified image with the specified size.
/// </summary>
/// <param name="original">The original to clone from.</param>
/// <param name="width">The width, in pixels, of the new <see cref="FreeImageBitmap"/>.</param>
/// <param name="height">The height, in pixels, of the new <see cref="FreeImageBitmap"/>.</param>
/// <exception cref="Exception">The operation failed.</exception>
/// <exception cref="ArgumentNullException"><paramref name="original"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="width"/> or <paramref name="height"/> are less or equal zero.</exception>
public FreeImageBitmap(Image original, int width, int height)
: this(original as Bitmap, width, height)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FreeImageBitmap"/> class
/// bases on the specified image.
/// </summary>
/// <param name="original">The original to clone from.</param>
/// <exception cref="Exception">The operation failed.</exception>
public FreeImageBitmap(Bitmap original)
{
if (original == null)
{
throw new ArgumentNullException("original");
}
dib = FreeImage.CreateFromBitmap(original, true);
if (dib.IsNull)
{
throw new Exception();
}
}
/// <summary>
/// Initializes a new instance of the <see cref="FreeImageBitmap"/> class
/// bases on the specified image with the specified size.
/// </summary>
/// <param name="original">The original to clone from.</param>
/// <param name="newSize">The Size structure that represent the
/// size of the new <see cref="FreeImageBitmap"/>.</param>
/// <exception cref="Exception">The operation failed.</exception>
/// <exception cref="ArgumentNullException"><paramref name="original"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="newSize.Width"/> or <paramref name="newSize.Height"/> are less or equal zero.
/// </exception>
public FreeImageBitmap(Bitmap original, Size newSize)
: this(original, newSize.Width, newSize.Height)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FreeImageBitmap"/> class
/// bases on the specified image with the specified size.
/// </summary>
/// <param name="original">The original to clone from.</param>
/// <param name="width">The width, in pixels, of the new <see cref="FreeImageBitmap"/>.</param>
/// <param name="height">The height, in pixels, of the new <see cref="FreeImageBitmap"/>.</param>
/// <exception cref="Exception">The operation failed.</exception>
/// <exception cref="ArgumentNullException"><paramref name="original"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="width"/> or <paramref name="height"/> are less or equal zero.</exception>
public FreeImageBitmap(Bitmap original, int width, int height)
{
if (original == null)
{
throw new ArgumentNullException("original");
}
if (width <= 0)
{
throw new ArgumentOutOfRangeException("width");
}
if (height <= 0)
{
throw new ArgumentOutOfRangeException("height");
}
FIBITMAP temp = FreeImage.CreateFromBitmap(original, true);
if (temp.IsNull)
{
throw new Exception();
}
else
{
dib = FreeImage.Rescale(temp, width, height, FREE_IMAGE_FILTER.FILTER_BICUBIC);
FreeImage.Unload(temp);
if (dib.IsNull)
{
throw new Exception();
}
}
}
/// <summary>
/// Initializes a new instance of the <see cref="FreeImageBitmap"/> class
/// bases on the specified stream.
/// </summary>
/// <param name="stream">Stream to read from.</param>
/// <param name="useIcm">Ignored.</param>
/// <exception cref="Exception">The operation failed.</exception>
/// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception>
public FreeImageBitmap(Stream stream, bool useIcm)
: this(stream)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FreeImageBitmap"/> class
/// bases on the specified stream.
/// </summary>
/// <param name="stream">Stream to read from.</param>
/// <exception cref="Exception">The operation failed.</exception>
/// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception>
public FreeImageBitmap(Stream stream)
: this(stream, FREE_IMAGE_FORMAT.FIF_UNKNOWN, FREE_IMAGE_LOAD_FLAGS.DEFAULT)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FreeImageBitmap"/> class
/// bases on the specified stream in the specified format.
/// </summary>
/// <param name="stream">Stream to read from.</param>
/// <param name="format">Format of the image.</param>
/// <exception cref="Exception">The operation failed.</exception>
/// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception>
public FreeImageBitmap(Stream stream, FREE_IMAGE_FORMAT format)
: this(stream, format, FREE_IMAGE_LOAD_FLAGS.DEFAULT)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FreeImageBitmap"/> class
/// bases on the specified stream with the specified loading flags.
/// </summary>
/// <param name="stream">Stream to read from.</param>
/// <param name="flags">Flags to enable or disable plugin-features.</param>
/// <exception cref="Exception">The operation failed.</exception>
/// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception>
public FreeImageBitmap(Stream stream, FREE_IMAGE_LOAD_FLAGS flags)
: this(stream, FREE_IMAGE_FORMAT.FIF_UNKNOWN, flags)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FreeImageBitmap"/> class
/// bases on the specified stream in the specified format
/// with the specified loading flags.
/// </summary>
/// <param name="stream">Stream to read from.</param>
/// <param name="format">Format of the image.</param>
/// <param name="flags">Flags to enable or disable plugin-features.</param>
/// <exception cref="Exception">The operation failed.</exception>
/// <exception cref="ArgumentNullException"><paramref name="stream"/> is null.</exception>
public FreeImageBitmap(Stream stream, FREE_IMAGE_FORMAT format, FREE_IMAGE_LOAD_FLAGS flags)
{
if (stream == null)
{
throw new ArgumentNullException("stream");
}
saveInformation.loadFlags = flags;
dib = FreeImage.LoadFromStream(stream, flags, ref format);
if (dib.IsNull)
{
throw new Exception();
}
originalFormat = format;
}
/// <summary>
/// Initializes a new instance of the <see cref="FreeImageBitmap"/> class bases on the specified file.
/// </summary>
/// <param name="filename">The complete name of the file to load.</param>
/// <exception cref="Exception">The operation failed.</exception>
/// <exception cref="ArgumentNullException"><paramref name="filename"/> is null.</exception>
/// <exception cref="FileNotFoundException"><paramref name="filename"/> does not exist.</exception>
public FreeImageBitmap(string filename)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -