📄 freeimagewrapper.cs
字号:
/// <param name="dib">Handle to a FreeImage bitmap.</param>
/// <param name="filename">The complete name of the file to save to.
/// The extension will be corrected if it is no valid extension for the
/// selected format or if no extension was specified.</param>
/// <param name="flags">Flags to enable or disable plugin-features.</param>
/// <param name="unloadSource">When true the structure will be unloaded on success.
/// If the function failed and returned false, the bitmap was not unloaded.</param>
/// <returns>Returns true on success, false on failure.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="dib"/> or <paramref name="filename"/> is null.</exception>
public static bool SaveEx(
ref FIBITMAP dib,
string filename,
FREE_IMAGE_SAVE_FLAGS flags,
bool unloadSource)
{
return SaveEx(
ref dib,
filename,
FREE_IMAGE_FORMAT.FIF_UNKNOWN,
flags,
FREE_IMAGE_COLOR_DEPTH.FICD_AUTO,
unloadSource);
}
/// <summary>
/// Saves a previously loaded FreeImage bitmap to a file.
/// In case the loading format is <see cref="FREE_IMAGE_FORMAT.FIF_UNKNOWN"/>
/// the format is taken off the filename.
/// If no suitable format was found false will be returned.
/// </summary>
/// <param name="dib">Handle to a FreeImage bitmap.</param>
/// <param name="filename">The complete name of the file to save to.
/// The extension will be corrected if it is no valid extension for the
/// selected format or if no extension was specified.</param>
/// <param name="format">Format of the image. If the format should be taken from the
/// filename use <see cref="FREE_IMAGE_FORMAT.FIF_UNKNOWN"/>.</param>
/// <param name="unloadSource">When true the structure will be unloaded on success.
/// If the function failed and returned false, the bitmap was not unloaded.</param>
/// <returns>Returns true on success, false on failure.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="dib"/> or <paramref name="filename"/> is null.</exception>
public static bool SaveEx(
ref FIBITMAP dib,
string filename,
FREE_IMAGE_FORMAT format,
bool unloadSource)
{
return SaveEx(
ref dib,
filename,
format,
FREE_IMAGE_SAVE_FLAGS.DEFAULT,
FREE_IMAGE_COLOR_DEPTH.FICD_AUTO,
unloadSource);
}
/// <summary>
/// Saves a previously loaded FreeImage bitmap to a file.
/// In case the loading format is <see cref="FREE_IMAGE_FORMAT.FIF_UNKNOWN"/>
/// the format is taken off the filename.
/// If no suitable format was found false will be returned.
/// Save flags can be provided by the flags parameter.
/// </summary>
/// <param name="dib">Handle to a FreeImage bitmap.</param>
/// <param name="filename">The complete name of the file to save to.
/// The extension will be corrected if it is no valid extension for the
/// selected format or if no extension was specified.</param>
/// <param name="format">Format of the image. If the format should be taken from the
/// filename use <see cref="FREE_IMAGE_FORMAT.FIF_UNKNOWN"/>.</param>
/// <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="filename"/> is null.</exception>
public static bool SaveEx(
FIBITMAP dib,
string filename,
FREE_IMAGE_FORMAT format,
FREE_IMAGE_SAVE_FLAGS flags)
{
return SaveEx(
ref dib,
filename,
format,
flags,
FREE_IMAGE_COLOR_DEPTH.FICD_AUTO,
false);
}
/// <summary>
/// Saves a previously loaded FreeImage bitmap to a file.
/// In case the loading format is <see cref="FREE_IMAGE_FORMAT.FIF_UNKNOWN"/>
/// the format is taken off the filename.
/// If no suitable format was found false will be returned.
/// Save flags can be provided by the flags parameter.
/// The bitmaps color depth can be set by 'colorDepth'.
/// If set to <see cref="FREE_IMAGE_COLOR_DEPTH.FICD_AUTO"/> a suitable color depth
/// will be taken if available.
/// </summary>
/// <param name="dib">Handle to a FreeImage bitmap.</param>
/// <param name="filename">The complete name of the file to save to.
/// The extension will be corrected if it is no valid extension for the
/// selected format or if no extension was specified.</param>
/// <param name="format">Format of the image. If the format should be taken from the
/// filename use <see cref="FREE_IMAGE_FORMAT.FIF_UNKNOWN"/>.</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 Save 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.
/// If the function failed and returned false, the bitmap was not unloaded.</param>
/// <returns>Returns true on success, false on failure.</returns>
/// <exception cref="ArgumentException">
/// A direct color conversion failed.</exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="dib"/> or <paramref name="filename"/> is null.</exception>
public static bool SaveEx(
ref FIBITMAP dib,
string filename,
FREE_IMAGE_FORMAT format,
FREE_IMAGE_SAVE_FLAGS flags,
FREE_IMAGE_COLOR_DEPTH colorDepth,
bool unloadSource)
{
if (dib.IsNull)
{
throw new ArgumentNullException("dib");
}
if (filename == null)
{
throw new ArgumentNullException("filename");
}
bool result = false;
// Gets format from filename if the format is unknown
if (format == FREE_IMAGE_FORMAT.FIF_UNKNOWN)
{
format = GetFIFFromFilename(filename);
}
if (format != FREE_IMAGE_FORMAT.FIF_UNKNOWN)
{
// Checks writing support
if (FIFSupportsWriting(format) && FIFSupportsExportType(format, GetImageType(dib)))
{
// Check valid filename and correct it if needed
if (!IsFilenameValidForFIF(format, filename))
{
int index = filename.LastIndexOf('.');
string extension = GetPrimaryExtensionFromFIF(format);
if (index == -1)
{
// We have no '.' (dot) so just add the extension
filename += "." + extension;
}
else
{
// Overwrite the old extension
filename = filename.Substring(0, filename.LastIndexOf('.')) + extension;
}
}
FIBITMAP dibToSave = PrepareBitmapColorDepth(dib, format, colorDepth);
result = Save(format, dibToSave, filename, flags);
// 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;
}
/// <summary>
/// Loads a FreeImage bitmap.
/// The stream must be set to the correct position before calling LoadFromStream.
/// </summary>
/// <param name="stream">The stream to read from.</param>
/// <returns>Handle to a FreeImage bitmap.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="stream"/> is null.</exception>
/// <exception cref="ArgumentException">
/// <paramref name="stream"/> is not capable of reading.</exception>
public static FIBITMAP LoadFromStream(
Stream stream)
{
FREE_IMAGE_FORMAT format = FREE_IMAGE_FORMAT.FIF_UNKNOWN;
return LoadFromStream(stream, FREE_IMAGE_LOAD_FLAGS.DEFAULT, ref format);
}
/// <summary>
/// Loads a FreeImage bitmap.
/// The stream must be set to the correct position before calling LoadFromStream.
/// </summary>
/// <param name="stream">The stream to read from.</param>
/// <param name="flags">Flags to enable or disable plugin-features.</param>
/// <returns>Handle to a FreeImage bitmap.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="stream"/> is null.</exception>
/// <exception cref="ArgumentException">
/// <paramref name="stream"/> is not capable of reading.</exception>
public static FIBITMAP LoadFromStream(
Stream stream,
FREE_IMAGE_LOAD_FLAGS flags)
{
FREE_IMAGE_FORMAT format = FREE_IMAGE_FORMAT.FIF_UNKNOWN;
return LoadFromStream(stream, flags, ref format);
}
/// <summary>
/// Loads a FreeImage bitmap.
/// In case the loading format is <see cref="FREE_IMAGE_FORMAT.FIF_UNKNOWN"/> the
/// bitmaps real format is being analysed.
/// The stream must be set to the correct position before calling LoadFromStream.
/// </summary>
/// <param name="stream">The stream to read from.</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 LoadFromStream it will be returned in format.</param>
/// <returns>Handle to a FreeImage bitmap.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="stream"/> is null.</exception>
/// <exception cref="ArgumentException">
/// <paramref name="stream"/> is not capable of reading.</exception>
public static FIBITMAP LoadFromStream(
Stream stream,
ref FREE_IMAGE_FORMAT format)
{
return LoadFromStream(stream, FREE_IMAGE_LOAD_FLAGS.DEFAULT, ref format);
}
/// <summary>
/// Loads a FreeImage bitmap.
/// In case the loading format is <see cref="FREE_IMAGE_FORMAT.FIF_UNKNOWN"/>
/// the bitmaps real format is being analysed.
/// The stream must be set to the correct position before calling LoadFromStream.
/// </summary>
/// <param name="stream">The stream to read from.</param>
/// <param name="flags">Flags to enable or disable plugin-features.</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 LoadFromStream it will be returned in format.</param>
/// <returns>Handle to a FreeImage bitmap.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="stream"/> is null.</exception>
/// <exception cref="ArgumentException">
/// <paramref name="stream"/> is not capable of reading.</exception>
public static FIBITMAP LoadFromStream(
Stream stream,
FREE_IMAGE_LOAD_FLAGS flags,
ref FREE_IMAGE_FORMAT format)
{
if (stream == null)
{
throw new ArgumentNullException("stream");
}
if (!stream.CanRead)
{
throw new ArgumentException("stream is not capable of reading.");
}
// Wrap the source stream if it is unable to seek (which is required by FreeImage)
stream = (stream.CanSeek) ? stream : new StreamWrapper(stream, true);
// Save the streams position
if (format == FREE_IMAGE_FORMAT.FIF_UNKNOWN)
{
long position = stream.Position;
// Get the format of the bitmap
format = GetFileTypeFromStream(stream);
// Restore the streams position
stream.Position = position;
}
if (!FIFSupportsReading(format))
{
return 0;
}
// Create a 'FreeImageIO' structure for calling 'LoadFromHandle'
// using the internal structure 'FreeImageStreamIO'.
FreeImageIO io = FreeImageStreamIO.io;
using (fi_handle handle = new fi_handle(stream))
{
return LoadFromHandle(format, ref io, handle, flags);
}
}
/// <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>
/// <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)
{
return SaveToStream(
ref dib,
stream,
format,
FREE_IMAGE_SAVE_FLAGS.DEFAULT,
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="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,
bool unloadSource)
{
return SaveToStream(
ref dib,
stream,
format,
FREE_IMAGE_SAVE_FLAGS.DEFAULT,
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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -