←Select platform

LoadItem Method

Summary

Load the image data for an item.

Syntax
C#
C++/CLI
protected abstract object LoadItem( 
   ImageViewerItem item 
) 
protected:  
   virtual Object^ LoadItem( 
      ImageViewerItem^ item 
   ) abstract 

Parameters

item

Source item

Return Value

The image object for this item.

Remarks

This method is called when an item comes into view and is not cached in memory.

Implementers must load the image data from the external source and return it here. This can be one of the following:

You should encapsulate the code inside this method with a try/catch and handle the error correctly. This method will not automatically fire the ItemError event.

Example
C#
using Leadtools; 
using Leadtools.Controls; 
using Leadtools.Codecs; 
using Leadtools.Drawing; 
 
 
public ImageViewerForm _form = new ImageViewerForm(); 
public ImageViewer _imageViewer; 
                   
public void ImageViewerVirtualizerExample() 
{ 
   // Get the input file name 
   string _imageFileName = imageFileName; 
 
   // Get the ImageViewer control from the form 
   _imageViewer = _form.ImageViewer; 
   // Set to vertical view layout 
   _imageViewer.ViewLayout = new ImageViewerVerticalViewLayout(); 
   // Add pan zoom interactive mode 
   _imageViewer.InteractiveModes.Add(new ImageViewerPanZoomInteractiveMode()); 
 
   // Add empty items, each one with the correct size but with no image data 
   // The virtualizer will take care of loading the pages and rendering them 
   using (RasterCodecs codecs = new RasterCodecs()) 
   { 
      int pageCount; 
      LeadSize pageSize; 
      LeadSizeD pageResolution; 
 
      using (CodecsImageInfo info = codecs.GetInformation(_imageFileName, true)) 
      { 
         pageCount = info.TotalPages; 
         pageSize = LeadSize.Create(info.Width, info.Height); 
         pageResolution = LeadSizeD.Create(info.XResolution, info.YResolution); 
      } 
 
      _imageViewer.BeginUpdate(); 
      for (int page = 1; page <= pageCount; page++) 
      { 
         ImageViewerItem item = new ImageViewerItem(); 
         item.ImageSize = pageSize; 
         item.Resolution = pageResolution; 
         _imageViewer.Items.Add(item); 
      } 
      _imageViewer.EndUpdate(); 
 
      // Set the virtualizer to start loading the data into the empty items 
      _imageViewer.Virtualizer = new MyVirtualizer(_imageFileName); 
 
      // Hook to the VirtualizeItemReady to log each item as its image data is loaded 
      _imageViewer.VirtualizeItemReady += (Object sender, ImageViewerVirtualizerEventArgs e) => 
      { 
         System.Diagnostics.Debug.WriteLine("Image data for page " + (e.Item.RowIndex + 1) + " is ready");  
      }; 
   } 
} 
 
// Custom Virtualizer 
public class MyVirtualizer : ImageViewerVirtualizer 
{ 
   private string _imageFileName; 
 
   public MyVirtualizer(string imageFileName) : base() 
   { 
      _imageFileName = imageFileName; 
      // Load 4 items at a time 
      this.MaximumItems = 4; 
   } 
 
   protected override object LoadItem(ImageViewerItem item) 
   { 
      // Load a page, the page number is the item index, but it 
      // could be anything in the application, for example 
      // using ImageViewerItem.Tag to store the necessary data to identity this item 
      int pageNumber = this.ImageViewer.Items.IndexOf(item); 
      using (RasterCodecs codecs = new RasterCodecs()) 
      { 
         return codecs.Load(_imageFileName, pageNumber); 
      } 
   } 
 
   protected override void SaveItem(ImageViewerItem item, object data) 
   { 
      // Nothing to do in this application, just log it. 
      // If save is required, then this is the place to do it 
      int pageNumber = this.ImageViewer.Items.IndexOf(item); 
      System.Diagnostics.Debug.WriteLine("SaveItem for page " + pageNumber + " is called"); 
   } 
 
   protected override void DeleteItem(ImageViewerItem item, object data) 
   { 
      // Dispose the image 
      RasterImage image = data as RasterImage; 
      if (image != null) 
         image.Dispose(); 
   } 
 
   protected override void RenderItemPlaceholder(ImageViewerRenderEventArgs e) 
   { 
      // Render the placeholder for this page 
      int pageNumber = this.ImageViewer.Items.IndexOf(e.Item) + 1; 
      LeadMatrix transform = this.ImageViewer.GetItemImageTransform(e.Item); 
 
      Graphics graphics = e.PaintEventArgs.Graphics; 
 
      // Render the message at 0,0 in this item transformation 
      LeadPointD pt = LeadPointD.Create(0, 0); 
      pt = transform.Transform(pt); 
      string message = string.Format("Loading page {0}...", pageNumber); 
      graphics.DrawString(message, this.ImageViewer.Font, Brushes.Black, (float)pt.X, (float)pt.Y); 
   } 
} 
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.Controls Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.