←Select platform

LoadAsync(string,int,object) Method

Summary
Loads the specified page from an image file using default options asynchronously.
Syntax
C#
Objective-C
C++/CLI
Python
public RasterImage LoadAsync( 
   string fileName, 
   int pageNumber, 
   object userState 
) 
- (void)loadFileAsync:(NSString *)file  
           pageNumber:(NSInteger)pageNumber  
           completion:(void (^)(LTRasterImage * __nullable image, NSError * __nullable error))completion 
public: 
RasterImage^ LoadAsync(  
   String^ fileName, 
   int pageNumber, 
   Object^ userState 
)  
def LoadAsync(self,fileName,pageNumber,userState): 

Parameters

fileName
A String containing the name of the image file to load.

pageNumber
The 1-based page number.

userState
A user-defined object that is passed to the method invoked when the asynchronous operation completes.

Return Value

The RasterImage object that this method loads. See remarks for the usage of this object.

Remarks

NOTE: This topic is part of RasterCodecs Async support using the .NET System.ComponentMode.AsyncOperation model. For .NET async/await, use Task<CodecsImageInfo> LoadAsync(ILeadStream stream, int bitsPerPixel).

The file can be in any supported image file format and bits per pixel, whether compressed or uncompressed.

This method will load a single page from a multipage file. The resulting image will have the same bits/pixel and color order value of the image as it was stored in the file.

LEADTOOLS will attempt to load corrupted files so you can see at least a portion of the image. For these images, the load methods succeed, but LoadStatus will return an error code.

Use the CodecsLoadOptions class to set up other load option parameters before calling this method.

Support for 12 and 16-bit grayscale images is only available in the Document/Medical Imaging editions.

For supported formats, refer to Summary of All Supported File Formats.

LEADTOOLS loads all PDF files as Raster PDF uncompressed RasterImageFormat.RasPdf, regardless of the compression and color space used when saving the file.

The RasterCodecs class supports loading image files asynchronously using the LoadAsync methods. When calling any of these methods, the caller thread will not be blocked and the method will return instantly with an instance of RasterImage that is in a loading status (RasterImage.IsLoading set to true). You should not use the other properties of this object while the object is in loading status.

When the RasterCodecs object finishes loading the image, the various properties of the RasterImage will be populated with the image information and data and the RasterImage.IsLoading property will be set to false.

Do not poll the RasterImage.IsLoading property to determine whether the image has finished loading. Instead, subscribe to the LoadAsyncCompleted event to get notification when the LoadAsync operation has completed and whether any errors occurred.

The LoadAsyncCompleted event data will also contain the same object returned from LoadAsync so you do not have to keep the original object in your application.

Example

This example will query the number of pages from a file and then load them.

C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.Svg; 
 
 
class LoadPagesFromFileAsyncTestData 
{ 
   public int PageCount; 
   public int PageNumber; 
} 
 
private static void LoadPagesFromFileAsyncTest(string fileName) 
{ 
   // Create a new RasterCodecs instance, we will dispose it when the images finish loading 
   RasterCodecs rasterCodecs = new RasterCodecs(); 
 
   // Create the data 
   LoadPagesFromFileAsyncTestData data = new LoadPagesFromFileAsyncTestData(); 
 
   // Get the number of pages 
   data.PageCount = rasterCodecs.GetTotalPages(fileName); 
   Debug.WriteLine("Number of pages in the file is {0}", data.PageCount); 
 
   // Load all pages asynchrounsly 
   data.PageNumber = 1; 
 
   // Start with the first page, then load next when operation finishes 
   rasterCodecs.LoadAsyncCompleted += new EventHandler<CodecsLoadAsyncCompletedEventArgs>(rasterCodecs_LoadAsyncCompleted); 
   rasterCodecs.LoadAsync(fileName, data.PageNumber, data); 
} 
 
private static void rasterCodecs_LoadAsyncCompleted(object sender, CodecsLoadAsyncCompletedEventArgs e) 
{ 
   // A page is loaded, show info 
   LoadPagesFromFileAsyncTestData data = e.UserState as LoadPagesFromFileAsyncTestData; 
   Debug.WriteLine("Page {0} loaded", data.PageNumber); 
 
   // If there is an error, show it 
   if (e.Error != null) 
      Debug.WriteLine("Error {0}", e.Error.Message); 
 
   // Dispose the image 
   if (e.Image != null) 
      e.Image.Dispose(); 
 
   // Get the raster codecs instance 
   RasterCodecs rasterCodecs = sender as RasterCodecs; 
 
   if (data.PageNumber < data.PageCount) 
   { 
      // Load next 
      Debug.WriteLine("Load next"); 
      data.PageNumber++; 
      rasterCodecs.LoadAsync(e.FileName, data.PageNumber, data); 
   } 
   else 
   { 
      // Finished loading, clean up 
      Debug.WriteLine("Finished"); 
      rasterCodecs.LoadAsyncCompleted -= new EventHandler<CodecsLoadAsyncCompletedEventArgs>(rasterCodecs_LoadAsyncCompleted); 
      rasterCodecs.Dispose(); 
   } 
} 
 
import java.io.*; 
import java.net.*; 
import java.nio.file.Paths; 
import java.util.*; 
import java.time.Instant; 
import java.time.Duration; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.codecs.RasterCodecs.FeedCallbackThunk; 
import leadtools.drawing.internal.*; 
import leadtools.imageprocessing.*; 
import leadtools.imageprocessing.color.ChangeIntensityCommand; 
import leadtools.svg.*; 
 
 
class LoadPagesFromFileAsyncTestData { 
   public int PageCount; 
   public int PageNumber; 
} 
 
ILeadStream fileStream; 
 
public void loadPagesFromFileAsyncExample() throws IOException { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   String fileName = combine(LEAD_VARS_IMAGES_DIR, "Leadtools.pdf"); 
   RasterCodecs rasterCodecs = new RasterCodecs(); 
 
   // Create the data 
   LoadPagesFromFileAsyncTestData data = new LoadPagesFromFileAsyncTestData(); 
 
   // Get the number of pages 
   data.PageCount = rasterCodecs.getTotalPages(fileName); 
   System.out.printf("Number of pages in the file is %s%n", data.PageCount); 
 
   // Load all pages asynchrounsly 
   data.PageNumber = 1; 
 
   // Start with the first page, then load next when operation finishes 
   rasterCodecs.addLoadAsyncCompletedListener(rasterCodecsLoadAsyncCompleted); 
   fileStream = LeadStreamFactory.create(fileName); 
   rasterCodecs.loadAsync(fileStream, data.PageNumber, data); 
} 
 
CodecsLoadAsyncCompletedListener rasterCodecsLoadAsyncCompleted = new CodecsLoadAsyncCompletedListener() { 
 
   @Override 
   public void onLoadAsyncCompleted(CodecsLoadAsyncCompletedEvent e) { 
      // A page is loaded, show info 
      LoadPagesFromFileAsyncTestData data = (LoadPagesFromFileAsyncTestData) e.getUserState(); 
      System.out.printf("Page %s loaded%n", data.PageNumber); 
 
      // If there is an error, show it 
      if (e.getError() != null) { 
         System.out.printf("Error %s%n", e.getError().getMessage()); 
      } 
      // Dispose the image 
      if (e.getImage() != null) { 
         e.getImage().dispose(); 
      } 
      // Get the raster codecs instance 
      RasterCodecs rasterCodecs = (RasterCodecs) e.getSource(); 
 
      if (data.PageNumber < data.PageCount) { 
         // Load next 
         System.out.println("Load next"); 
         data.PageNumber++; 
         rasterCodecs.loadAsync(fileStream, data.PageNumber, data); 
      } else { 
         // Finished loading, clean up 
         System.out.println("Finished"); 
         rasterCodecs.removeLoadAsyncCompletedListener(rasterCodecsLoadAsyncCompleted); 
         rasterCodecs.dispose(); 
      } 
   } 
 
}; 
Requirements

Target Platforms

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Codecs Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.