ImageGear for Java User Guide > Using ImageGear for Java > Loading/Saving Pages and Documents |
ImGearFileFormats Class should be used to load/save images. It offers the LoadPage, SavePage, LoadDocument and SaveDocument methods for these purposes. Each of these members loads or saves an image from or to an IGStream object.
ImageGear only supports saving to streams that have the seek ability. The following example demonstrates how to load a single page from a local file:Example |
Copy Code |
---|---|
ImGearFileStream fileStream = null; ImGearPage page = null; try { fileStream = new ImGearFileStream("photo.jpg", "r"); page = ImGearFileFormats.LoadPage(fileStream, 0); } finally { if (fileStream != null) { fileStream.close(); } } |
The next examples demonstrates how to load an image from a URL.
Example |
Copy Code |
---|---|
URLConnection connection = null; URL serverAddress = null; InputStream inputStream = null; IGMemoryStream igms = null; try { serverAddress = new URL("http://www.google.com/intl/en_ALL/images/logo.gif"); connection = (URLConnection) serverAddress.openConnection(); inputStream = connection.getInputStream(); igms = new IGMemoryStream(); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer, 0, buffer.length)) != -1) { igms.Write(buffer, 0, bytesRead); } igms.setPosition(0); ImGearPage page = ImGearFileFormats.LoadPage(igms, 0); if (page != null) { // do something with the image } } finally { if (inputStream != null) { inputStream.close(); } } |
The next example demonstrates how to save a particular page of a multipage document into a separate file:
Example |
Copy Code |
---|---|
ImGearDocument igDocument; ImGearFileStream localFile = null; ImGearFileStream output = null; try { localFile = new ImGearFileStream("multipage.tif", "r"); int pageCount = ImGearFileFormats.GetPageCount(localFile, ImGearFormats.UNKNOWN); igDocument = ImGearFileFormats.LoadDocument(localFile, 0, pageCount); if (igDocument != null) { output = new ImGearFileStream("firstPage.tif", "rw"); ImGearFileFormats.SavePage(igDocument.getPages().getItem(0), output, 0, ImGearSavingModes.OVERWRITE, ImGearSavingFormats.TIF_G4); } } finally { if (localFile != null) { localFile.close(); } if (output != null) { output.close(); } } |
Also refer to the following sections: