User Guide > How to Work with... > Common Operations > Loading and Saving > Loading |
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. |
To load a single page, use the LoadPage method as follows:
C# |
Copy Code |
---|---|
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; } |
VB.NET |
Copy Code |
---|---|
Imports System.IO
Imports ImageGear.Core
Imports ImageGear.Formats
Public Function LoadPageFromLocalFile(filePath As String) As ImGearPage
' From a local file
Dim igPage As ImGearPage
Dim pageIndex As Integer = 0
Using localFile As New FileStream(filePath, FileMode.Open)
igPage = ImGearFileFormats.LoadPage(localFile, pageIndex)
End Using
Return igPage
End Function |
ImageGear provides a specialized stream class for loading of images from the internet: ImGearWebStream class. This class is most useful for loading multi-page images, because it downloads only the necessary portion of a remote file. ImGearWebStream class only supports loading images from servers that use HTTP protocol. The following example demonstrates how to load a page from a remote document using ImGearWebStream class:
C# |
Copy Code |
---|---|
using ImageGear.Core; using ImageGear.Formats; public ImGearPage LoadPageFromWebServerFile(string webServerFileURL) { ImGearPage igPage; int pageIndex = 0; using (ImGearWebStream igWebStream = new ImGearWebStream(webServerFileURL)) igPage = ImGearFileFormats.LoadPage(igWebStream, pageIndex); return igPage; } |
VB.NET |
Copy Code |
---|---|
Imports ImageGear.Core Imports ImageGear.Formats Public Function LoadPageFromWebServerFile(webServerFileURL As String) As ImGearPage Dim igPage As ImGearPage Dim pageIndex As Integer = 0 Using igWebStream As New ImGearWebStream(webServerFileURL) igPage = ImGearFileFormats.LoadPage(igWebStream, pageIndex) End Using Return igPage End Function |
Furthermore, we can load a page from a remote file using WebRequest:
C# |
Copy Code |
---|---|
using System.Net; using ImageGear.Core; using ImageGear.Formats; public ImGearPage LoadPageFromWebRequestrFile(string webServerFileURL) { ImGearPage igPage; int pageIndex = 0; WebRequest request = WebRequest.Create(webServerFileURL); using (WebResponse response = request.GetResponse()) using (Stream stream = response.GetResponseStream()) igPage = ImGearFileFormats.LoadPage(stream, pageIndex); return igPage; } |
VB.NET |
Copy Code |
---|---|
Imports System.Net Imports ImageGear.Core Imports ImageGear.Formats Public Function LoadPageFromWebRequestrFile(webServerFileURL As String) As ImGearPage Dim igPage As ImGearPage Dim pageIndex As Integer = 0 Dim request As WebRequest = WebRequest.Create(webServerFileURL) Using response As WebResponse = request.GetResponse() Using stream As Stream = response.GetResponseStream() igPage = ImGearFileFormats.LoadPage(stream, pageIndex) End Using End Using Return igPage End Function |
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 .NET:
C# |
Copy Code |
---|---|
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; } |
VB.NET |
Copy Code |
---|---|
Imports System.IO Imports ImageGear.Core Imports ImageGear.Formats Public Function LoadDocumentFromFile(filePath As String) As ImGearDocument Dim igDocument As ImGearDocument Using localFile As New FileStream(filePath, FileMode.Open) Dim pageCount As Integer = ImGearFileFormats.GetPageCount(localFile, ImGearFormats.UNKNOWN) igDocument = ImGearFileFormats.LoadDocument(localFile, 0, pageCount) End Using Return igDocument End Function |
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.
We can use ImGearLoadOptions for setting certain options related to a loading operation.
The following example demonstrates how to load a Camera Raw format file by setting an instance of ImGearRawLoadOptions object. For this purpose, ImageGear provides a special dialog OpenRawFileDialog.
C# |
Copy Code |
---|---|
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); } } |
VB.NET |
Copy Code |
---|---|
Imports ImageGear.Core Imports ImageGear.Formats Imports ImageGear.Windows.Forms Public Sub LoadRawImage() ' Create and Fill an instance of ImGearRawLoadOptions Dim optionsForLoadRawFile As ImGearRawLoadOptions = Nothing 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 dialog As 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(Me) = System.Windows.Forms.DialogResult.OK Then 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 End If End Using Using fileStream As New FileStream(filePathNameForLoadRawFile, FileMode.Open, FileAccess.Read) imGearPage = ImGearFileFormats.LoadPage(fileStream, 0, optionsForLoadRawFile) End Using End Sub |