📄 freeimagewrapper.cs
字号:
/// <param name="flags">Flags to enable or disable plugin-features.</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(
FIBITMAP dib,
Stream stream,
FREE_IMAGE_FORMAT format,
FREE_IMAGE_SAVE_FLAGS flags)
{
return SaveToStream(
ref dib,
stream,
format,
flags,
FREE_IMAGE_COLOR_DEPTH.FICD_AUTO,
false);
}
/// <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="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,
bool unloadSource)
{
return SaveToStream(
ref dib, stream,
format,
flags,
FREE_IMAGE_COLOR_DEPTH.FICD_AUTO,
unloadSource);
}
/// <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>
/// <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(
FIBITMAP dib,
Stream stream,
FREE_IMAGE_FORMAT format,
FREE_IMAGE_SAVE_FLAGS flags,
FREE_IMAGE_COLOR_DEPTH colorDepth)
{
return SaveToStream(
ref dib,
stream,
format,
flags,
colorDepth,
false);
}
/// <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;
}
#endregion
#region Plugin functions
/// <summary>
/// Checks if an extension is valid for a certain format.
/// </summary>
/// <param name="fif">The desired format.</param>
/// <param name="extension">The desired extension.</param>
/// <returns>True if the extension is valid for the given format, false otherwise.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="extension"/> is null.</exception>
public static bool IsExtensionValidForFIF(FREE_IMAGE_FORMAT fif, string extension)
{
return IsExtensionValidForFIF(fif, extension, StringComparison.CurrentCulture);
}
/// <summary>
/// Checks if an extension is valid for a certain format.
/// </summary>
/// <param name="fif">The desired format.</param>
/// <param name="extension">The desired extension.</param>
/// <param name="comparisonType">The string comparison type.</param>
/// <returns>True if the extension is valid for the given format, false otherwise.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="extension"/> is null.</exception>
public static bool IsExtensionValidForFIF(FREE_IMAGE_FORMAT fif, string extension, StringComparison comparisonType)
{
if (extension == null)
{
throw new ArgumentNullException("extension");
}
bool result = false;
// Split up the string and compare each with the given extension
string tempList = GetFIFExtensionList(fif);
if (tempList != null)
{
string[] extensionList = tempList.Split(',');
foreach (string ext in extensionList)
{
if (extension.Equals(ext, comparisonType))
{
result = true;
break;
}
}
}
return result;
}
/// <summary>
/// Checks if a filename is valid for a certain format.
/// </summary>
/// <param name="fif">The desired format.</param>
/// <param name="filename">The desired filename.</param>
/// <returns>True if the filename is valid for the given format, false otherwise.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="filename"/> is null.</exception>
public static bool IsFilenameValidForFIF(FREE_IMAGE_FORMAT fif, string filename)
{
return IsFilenameValidForFIF(fif, filename, StringComparison.CurrentCulture);
}
/// <summary>
/// Checks if a filename is valid for a certain format.
/// </summary>
/// <param name="fif">The desired format.</param>
/// <param name="filename">The desired filename.</param>
/// <param name="comparisonType">The string comparison type.</param>
/// <returns>True if the filename is valid for the given format, false otherwise.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="filename"/> is null.</exception>
public static bool IsFilenameValidForFIF(FREE_IMAGE_FORMAT fif, string filename, StringComparison comparisonType)
{
if (filename == null)
{
throw new ArgumentNullException("filename");
}
bool result = false;
// Extract the filenames extension if it exists
int position = filename.LastIndexOf('.');
if (position >= 0)
{
result = IsExtensionValidForFIF(fif, filename.Substring(position + 1), comparisonType);
}
return result;
}
/// <summary>
/// This function returns the primary (main or most commonly used?) extension of a certain
/// image format (fif). This is done by returning the first of all possible extensions
/// returned by GetFIFExtensionList().
/// That assumes, that the plugin returns the extensions in ordered form.</summary>
/// <param name="fif">The image format to obtain the primary extension for.</param>
/// <returns>The primary extension of the specified image format.</returns>
public static string GetPrimaryExtensionFromFIF(FREE_IMAGE_FORMAT fif)
{
string result = null;
string extensions = GetFIFExtensionList(fif);
if (extensions != null)
{
int position = extensions.IndexOf(',');
if (position < 0)
{
result = extensions;
}
else
{
result = extensions.Substring(0, position);
}
}
return result;
}
#endregion
#region Multipage functions
/// <summary>
/// Loads a FreeImage multi-paged bitmap.
/// </summary>
/// <param name="filename">The complete name of the file to load.</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)
{
FREE_IMAGE_FORMAT format = FREE_IMAGE_FORMAT.FIF_UNKNOWN;
return OpenMultiBitmapEx(
filename,
ref format,
FREE_IMAGE_LOAD_FLAGS.DEFAULT,
false,
false,
false);
}
/// <summary>
/// Loads a FreeImage multi-paged bitmap.
/// </summary>
/// <param name="filename">The complete name of the file to load.</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,
bool keep_cache_in_memory)
{
FREE_IMAGE_FORMAT format = FREE_IMAGE_FORMAT.FIF_UNKNOWN;
return OpenMultiBitmapEx(
filename,
ref format,
FREE_IMAGE_LOAD_FLAGS.DEFAULT,
false,
false,
keep_cache_in_memory);
}
/// <summary>
/// Loads a FreeImage multi-paged bitmap.
/// </summary>
/// <param name="filename">The complete name of the file to load.</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,
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,
false,
read_only,
keep_cache_in_memory);
}
/// <summary>
/// Loads a FreeImage multi-paged bitmap.
/// </summary>
/// <param name="filename">The complete name of the file to load.</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(
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -