📄 freeimagewrapper.cs
字号:
string filename,
bool create_new,
bool read_only,
bool keep_cache_in_memory)
{
FREE_IMAGE_FORMAT format = FREE_IMAGE_FORMAT.FIF_UNKNOWN;
return OpenMultiBitmapEx(
filename,
ref format,
FREE_IMAGE_LOAD_FLAGS.DEFAULT,
create_new,
read_only,
keep_cache_in_memory);
}
/// <summary>
/// Loads a FreeImage multi-paged bitmap.
/// In case the loading format is <see cref="FREE_IMAGE_FORMAT.FIF_UNKNOWN"/> the files real
/// format is being analysed. If no plugin can read the file, format remains
/// <see cref="FREE_IMAGE_FORMAT.FIF_UNKNOWN"/> and 0 is returned.
/// </summary>
/// <param name="filename">The complete name of the file to load.</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 LoadEx it will be returned in format.</param>
/// <param name="create_new">When true a new bitmap is created.</param>
/// <param name="read_only">When true the bitmap will be loaded read only.</param>
/// <param name="keep_cache_in_memory">When true performance is increased at the cost of memory.</param>
/// <returns>Handle to a FreeImage multi-paged bitmap.</returns>
/// <exception cref="FileNotFoundException">
/// <paramref name="filename"/> does not exists while opening.</exception>
public static FIMULTIBITMAP OpenMultiBitmapEx(
string filename,
ref FREE_IMAGE_FORMAT format,
bool create_new,
bool read_only,
bool keep_cache_in_memory)
{
return OpenMultiBitmapEx(
filename,
ref format,
FREE_IMAGE_LOAD_FLAGS.DEFAULT,
create_new,
read_only,
keep_cache_in_memory);
}
/// <summary>
/// Loads a FreeImage multi-paged bitmap.
/// In case the loading format is <see cref="FREE_IMAGE_FORMAT.FIF_UNKNOWN"/> the files
/// real format is being analysed. If no plugin can read the file, format remains
/// <see cref="FREE_IMAGE_FORMAT.FIF_UNKNOWN"/> and 0 is returned.
/// Load flags can be provided by the flags parameter.
/// </summary>
/// <param name="filename">The complete name of the file to load.</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 LoadEx it will be returned in format.</param>
/// <param name="flags">Flags to enable or disable plugin-features.</param>
/// <param name="create_new">When true a new bitmap is created.</param>
/// <param name="read_only">When true the bitmap will be loaded read only.</param>
/// <param name="keep_cache_in_memory">When true performance is increased at the cost of memory.</param>
/// <returns>Handle to a FreeImage multi-paged bitmap.</returns>
/// <exception cref="FileNotFoundException">
/// <paramref name="filename"/> does not exists while opening.</exception>
public static FIMULTIBITMAP OpenMultiBitmapEx(
string filename,
ref FREE_IMAGE_FORMAT format,
FREE_IMAGE_LOAD_FLAGS flags,
bool create_new,
bool read_only,
bool keep_cache_in_memory)
{
if (!File.Exists(filename) && !create_new)
{
throw new FileNotFoundException(filename + " could not be found.");
}
if (format == FREE_IMAGE_FORMAT.FIF_UNKNOWN)
{
// Check if a plugin can read the data
format = GetFileType(filename, 0);
}
FIMULTIBITMAP dib = 0;
if (FIFSupportsReading(format))
{
dib = OpenMultiBitmap(format, filename, create_new, read_only, keep_cache_in_memory, flags);
}
return dib;
}
/// <summary>
/// Closes a previously opened multi-page bitmap and, when the bitmap was not opened read-only,
/// applies any changes made to it.
/// On success the handle will be reset to null.
/// </summary>
/// <param name="dib">Handle to a FreeImage multi-paged bitmap.</param>
/// <returns>Returns true on success, false on failure.</returns>
public static bool CloseMultiBitmapEx(ref FIMULTIBITMAP dib)
{
return CloseMultiBitmapEx(ref dib, FREE_IMAGE_SAVE_FLAGS.DEFAULT);
}
/// <summary>
/// Closes a previously opened multi-page bitmap and, when the bitmap was not opened read-only,
/// applies any changes made to it.
/// On success the handle will be reset to null.
/// </summary>
/// <param name="dib">Handle to a FreeImage multi-paged bitmap.</param>
/// <param name="flags">Flags to enable or disable plugin-features.</param>
/// <returns>Returns true on success, false on failure.</returns>
public static bool CloseMultiBitmapEx(ref FIMULTIBITMAP dib, FREE_IMAGE_SAVE_FLAGS flags)
{
bool result = false;
if (!dib.IsNull)
{
if (CloseMultiBitmap(dib, flags))
{
dib = 0;
result = true;
}
}
return result;
}
/// <summary>
/// Retrieves the number of pages that are locked in a multi-paged bitmap.
/// </summary>
/// <param name="dib">Handle to a FreeImage multi-paged bitmap.</param>
/// <returns>Number of locked pages.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="dib"/> is null.</exception>
public static int GetLockedPageCount(FIMULTIBITMAP dib)
{
if (dib.IsNull)
{
throw new ArgumentNullException("dib");
}
int result = 0;
GetLockedPageNumbers(dib, null, ref result);
return result;
}
/// <summary>
/// Retrieves a list locked pages of a multi-paged bitmap.
/// </summary>
/// <param name="dib">Handle to a FreeImage multi-paged bitmap.</param>
/// <returns>List containing the indexes of the locked pages.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="dib"/> is null.</exception>
public static int[] GetLockedPages(FIMULTIBITMAP dib)
{
if (dib.IsNull)
{
throw new ArgumentNullException("dib");
}
// Get the number of pages and create an array to save the information
int count = 0;
int[] result = null;
// Get count
if (GetLockedPageNumbers(dib, result, ref count))
{
result = new int[count];
// Fill array
if (!GetLockedPageNumbers(dib, result, ref count))
{
result = null;
}
}
return result;
}
/// <summary>
/// Loads a FreeImage multi-paged bitmap from a stream and returns the
/// FreeImage memory stream used as temporary buffer.
/// The bitmap can not be modified by calling
/// <see cref="FreeImage.AppendPage(FIMULTIBITMAP,FIBITMAP)"/>,
/// <see cref="FreeImage.InsertPage(FIMULTIBITMAP,Int32,FIBITMAP)"/>,
/// <see cref="FreeImage.MovePage(FIMULTIBITMAP,Int32,Int32)"/> or
/// <see cref="FreeImage.DeletePage(FIMULTIBITMAP,Int32)"/>.
/// </summary>
/// <param name="stream">The stream to read from.</param>
/// <param name="format">Format of the image.</param>
/// <param name="flags">Flags to enable or disable plugin-features.</param>
/// <param name="memory">The temporary memory buffer used to load the bitmap.</param>
/// <returns>Handle to a FreeImage multi-paged bitmap.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="stream"/> is null.</exception>
/// <exception cref="ArgumentException">
/// <paramref name="stream"/> can not read.</exception>
public static FIMULTIBITMAP LoadMultiBitmapFromStream(
Stream stream,
FREE_IMAGE_FORMAT format,
FREE_IMAGE_LOAD_FLAGS flags,
out FIMEMORY memory)
{
if (stream == null)
{
throw new ArgumentNullException("stream");
}
if (!stream.CanRead)
{
throw new ArgumentException("stream");
}
const int blockSize = 1024;
int bytesRead;
byte[] buffer = new byte[blockSize];
stream = stream.CanSeek ? stream : new StreamWrapper(stream, true);
memory = OpenMemory(IntPtr.Zero, 0);
do
{
bytesRead = stream.Read(buffer, 0, blockSize);
WriteMemory(buffer, (uint)blockSize, (uint)1, memory);
}
while (bytesRead == blockSize);
return LoadMultiBitmapFromMemory(format, memory, flags);
}
#endregion
#region Filetype functions
/// <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);
}
}
#endregion
#region Pixel access functions
/// <summary>
/// Retrieves an hBitmap for a FreeImage bitmap.
/// Call FreeHbitmap(IntPtr) to free the handle.
/// </summary>
/// <param name="dib">Handle to a FreeImage bitmap.</param>
/// <param name="hdc">A reference device context.
/// Use IntPtr.Zero if no reference is available.</param>
/// <param name="unload">When true dib will be unloaded if the function succeeded.</param>
/// <returns>The hBitmap for the FreeImage bitmap.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="dib"/> is null.</exception>
public static unsafe IntPtr GetHbitmap(FIBITMAP dib, IntPtr hdc, bool unload)
{
if (dib.IsNull)
{
throw new ArgumentNullException("dib");
}
IntPtr hBitmap = IntPtr.Zero;
bool release = false;
IntPtr ppvBits = IntPtr.Zero;
// Check if we have destination
if (release = (hdc == IntPtr.Zero))
{
// We don't so request dc
hdc = GetDC(IntPtr.Zero);
}
if (hdc != IntPtr.Zero)
{
// Get pointer to the infoheader of the bitmap
IntPtr info = GetInfo(dib);
// Create a bitmap in the dc
hBitmap = CreateDIBSection(hdc, info, DIB_RGB_COLORS, out ppvBits, IntPtr.Zero, 0);
if (hBitmap != IntPtr.Zero && ppvBits != IntPtr.Zero)
{
// Copy the data into the dc
CopyMemory(
ppvBits,
GetBits(dib),
(GetHeight(dib) * GetPitch(dib)));
// Success: we unload the bitmap
if (unload)
{
Unload(dib);
}
}
// We have to release the dc
if (release)
{
ReleaseDC(IntPtr.Zero, hdc);
}
}
return hBitmap;
}
/// <summary>
/// Returns an HBITMAP created by the <c>CreateDIBitmap()</c> function which in turn
/// has always the same color depth as the reference DC, which may be provided
/// through <paramref name="hdc"/>. The desktop DC will be used,
/// if <c>IntPtr.Zero</c> DC is specified.
/// Call <see cref="FreeImage.FreeHbitmap(IntPtr)"/> to free the handle.
/// </summary>
/// <param name="dib">Handle to a FreeImage bitmap.</param>
/// <param name="hdc">Handle to a device context.</param>
/// <param name="unload">When true the structure will be unloaded on success.
/// If the function failed and returned false, the bitmap was not unloaded.</param>
/// <returns>If the function succeeds, the return value is a handle to the
/// compatible bitmap. If the function fails, the return value is <see cref="IntPtr.Zero"/>.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="dib"/> is null.</exception>
public static IntPtr GetBitmapForDevice(FIBITMAP dib, IntPtr hdc, bool unload)
{
if (dib.IsNull)
{
throw new ArgumentNullException("dib");
}
IntPtr hbitmap = IntPtr.Zero;
bool release = false;
if (release = (hdc == IntPtr.Zero))
{
hdc = GetDC(IntPtr.Zero);
}
if (hdc != IntPtr.Zero)
{
hbitmap = CreateDIBitmap(
hdc,
GetInfoHeader(dib),
CBM_INIT,
GetBits(dib),
GetInfo(dib),
DIB_RGB_COLORS);
if (unload)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -