The ImGearFileFormats
class provides the APIs that should be employed to load a document/image. This operation is mainly performed from a given .NET Stream object. ImageGear supports loading from streams having the seek ability, as well as from streams that don't have the seek ability, such as network streams. If the loading operation cannot be performed, an exception with either of the following codes is thrown: ImageGear.Core.ImGearErrorCodes.CANT_DETECT_FORMAT
or ImageGear.Core.ImGearErrorCodes.CANT_FIND_FORMAT
.
If the document size is close to 2GB, the
System.OutOfMemoryException
will be thrown.
Load a Single Page
To load a single page, use the LoadPage
method as follows:
C#
using System.IO;
using ImageGear.Core;
using ImageGear.Formats;
public ImGearPage LoadPageFromLocalFile(string filePath)
{
// From a local file
ImGearPage igPage;
int pageIndex = 0;
using (FileStream localFile = new FileStream(filePath,FileMode.Open))
{
igPage = ImGearFileFormats.LoadPage(localFile, pageIndex);
}
return igPage;
}
Load a Document (Multiple Pages as a Document)
For this purpose, we should use the LoadDocument
method. See What's the Best Format for...? for a list of the multi-page formats supported by ImageGear:
C#
using System.IO;
using ImageGear.Core;
using ImageGear.Formats;
public ImGearDocument LoadDocumentFromFile(string filePath)
{
ImGearDocument igDocument;
using (FileStream localFile = new FileStream(filePath, FileMode.Open))
{
int pageCount = ImGearFileFormats.GetPageCount(localFile, ImGearFormats.UNKNOWN);
igDocument = ImGearFileFormats.LoadDocument(localFile, 0, pageCount);
}
return igDocument;
}
For both the LoadPage
method and the LoadDocument
method, there is an overload receiving another parameter, ImGearLoadOptions
object that allows you to be more specific about the loading operation.
Use Optional Parameters
We can use ImGearLoadOptions
for setting certain options related to a loading operation.
The following example demonstrates how to load a format file by setting an instance of ImGearRawLoadOptions
object. For this purpose, ImageGear provides a special dialog OpenRawFileDialog.
C#
using ImageGear.Core;
using ImageGear.Formats;
using ImageGear.Windows.Forms;
public void LoadRawImage()
{
// Create and Fill an instance of ImGearRawLoadOptions
ImGearRawLoadOptions optionsForLoadRawFile = null;
optionsForLoadRawFile = new ImGearRawLoadOptions();
optionsForLoadRawFile.Format = ImGearFormats.RAW;
optionsForLoadRawFile.Compression = ImGearCompressions.LZW;
optionsForLoadRawFile.ColorSpace = new ImGearColorSpace(ImGearColorSpaceIDs.Gy);
optionsForLoadRawFile.BitsPerChannel = 9;
optionsForLoadRawFile.Alignment = ImGearRawAlignment.DoubleWord;
optionsForLoadRawFile.Offset = 0;
optionsForLoadRawFile.Width = 400;
optionsForLoadRawFile.Height = 300;
filePathNameForLoadRawFile = string.Empty;
// Open a dialog especially designed for raw file formats.
// A common Open File Dialog can be used instead if desired.
using (OpenRawFileDialog dialog = new OpenRawFileDialog())
{
dialog.Compression = optionsForLoadRawFile.Compression;
dialog.ColorSpace = optionsForLoadRawFile.ColorSpace;
dialog.BitsPerChannel = optionsForLoadRawFile.BitsPerChannel;
dialog.Alignment = optionsForLoadRawFile.Alignment;
dialog.Offset = optionsForLoadRawFile.Offset;
dialog.ImageWidth = optionsForLoadRawFile.Width;
dialog.ImageHeight = optionsForLoadRawFile.Height;
dialog.FillOrder = optionsForLoadRawFile.FillOrder;
dialog.IsRasterPacked = optionsForLoadRawFile.IsRasterPacked;
dialog.FilePathName = filePathNameForLoadRawFile;
if (dialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
{
optionsForLoadRawFile.Compression = dialog.Compression;
optionsForLoadRawFile.ColorSpace = dialog.ColorSpace;
optionsForLoadRawFile.BitsPerChannel = dialog.BitsPerChannel;
optionsForLoadRawFile.Alignment = dialog.Alignment;
optionsForLoadRawFile.Offset = dialog.Offset;
optionsForLoadRawFile.Width = dialog.ImageWidth;
optionsForLoadRawFile.Height = dialog.ImageHeight;
optionsForLoadRawFile.FillOrder = dialog.FillOrder;
optionsForLoadRawFile.IsRasterPacked = dialog.IsRasterPacked;
filePathNameForLoadRawFile = dialog.FilePathName;
}
}
using (FileStream fileStream = new FileStream(filePathNameForLoadRawFile, FileMode.Open, FileAccess.Read))
{
imGearPage = ImGearFileFormats.LoadPage(fileStream, 0, optionsForLoadRawFile);
}
}