Products | Support | Email a link to this topic. | Send comments on this topic. | Back to Introduction - All Topics | Help Version 19.0.4.3
|
Leadtools.Controls Assembly > Leadtools.Controls Namespace : ImageViewerVirtualizer Class |
public abstract class ImageViewerVirtualizer
'Declaration
Public MustInherit Class ImageViewerVirtualizer
The ImageViewerVirtualizer provide support for handling large amount of image data in an ImageViewer by loading and unloading item data on demand with full control for rendering place-holders and controlling the number of items to cache in memory.
For detailed information, refer to Image Viewer Virtualizer.
Imports Leadtools Imports Leadtools.Controls Imports Leadtools.Codecs Imports Leadtools.Drawing <TestMethod> Public Sub ImageViewerVirtualizer_Example() Dim imageFileName As String = GetExampleImageFile() ' Create a new form to test out virtualizer Dim form As MyForm = New MyForm(imageFileName) form.ShowDialog() End Sub Private Shared Function GetExampleImageFile() As String Dim imageFileName As String = Path.Combine(ImagesPath.Path, "ImageViewerVirtualizerExample.tif") ' Check if it is previously created, return it If File.Exists(imageFileName) Then Return imageFileName End If ' Create an image that has many pages, we will use that for out testing Const pageCount As Integer = 100 Const imageWidth As Integer = 800 Const imageHeight As Integer = 800 Dim rc As New Rectangle(0, 0, imageWidth, imageHeight) Using codecs As RasterCodecs = New RasterCodecs() Using font As Font = New Font(FontFamily.GenericMonospace, 80) For page As Integer = 1 To pageCount Using image As RasterImage = RasterImage.Create(imageWidth, imageHeight, 24, 96, RasterColor.FromKnownColor(RasterKnownColor.White)) Dim hdc As IntPtr = RasterImagePainter.CreateLeadDC(image) Using graphics As Graphics = graphics.FromHdc(hdc) graphics.DrawRectangle(Pens.Blue, 0, 0, rc.Width - 1, rc.Height - 1) TextRenderer.DrawText(graphics, "Page " & page.ToString(), font, rc, Color.Red, TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenter) End Using RasterImagePainter.DeleteLeadDC(hdc) If page = 1 Then codecs.Save(image, imageFileName, RasterImageFormat.TifLzw, 0, 1, -1, -1, CodecsSavePageMode.Overwrite) Else codecs.Save(image, imageFileName, RasterImageFormat.TifLzw, 0, 1, -1, -1, CodecsSavePageMode.Append) End If End Using Next page End Using End Using Return imageFileName End Function ' Custom Virtualizer Public Class MyVirtualizer : Inherits ImageViewerVirtualizer Private _imageFileName As String Public Sub New(ByVal imageFileName As String) MyBase.New() _imageFileName = imageFileName ' We will load 4 items at a time Me.MaximumItems = 4 End Sub Protected Overrides Function LoadItem(ByVal item As ImageViewerItem) As Object ' Load a page, the page number is the item index + 1, but it ' could be anything in your application, for example ' using ImageViewerItem.Tag to store the necessary data to identity this item Dim pageNumber As Integer = Me.ImageViewer.Items.IndexOf(item) + 1 Using codecs As RasterCodecs = New RasterCodecs() Return codecs.Load(_imageFileName, pageNumber) End Using End Function Protected Overrides Sub SaveItem(ByVal item As ImageViewerItem, ByVal data As Object) ' Nothing to do in this application, just log it. ' If save is required, then this is the place to do it Dim pageNumber As Integer = Me.ImageViewer.Items.IndexOf(item) + 1 System.Diagnostics.Debug.WriteLine("SaveItem for page {0} is called") End Sub Protected Overrides Sub DeleteItem(ByVal item As ImageViewerItem, ByVal data As Object) ' Dispose the image Dim image As RasterImage = TryCast(data, RasterImage) If Not image Is Nothing Then image.Dispose() End If End Sub Protected Overrides Sub RenderItemPlaceholder(ByVal e As ImageViewerRenderEventArgs) ' Render the place holder for this page Dim pageNumber As Integer = Me.ImageViewer.Items.IndexOf(e.Item) + 1 Dim transform As LeadMatrix = Me.ImageViewer.GetItemImageTransform(e.Item) Dim graphics As Graphics = e.PaintEventArgs.Graphics ' Render the message at 0,0 in this item transformation Dim pt As LeadPointD = LeadPointD.Create(0, 0) pt = transform.Transform(pt) Dim message As String = String.Format("Loading page {0}...", pageNumber) graphics.DrawString(message, Me.ImageViewer.Font, Brushes.Black, CSng(pt.X), CSng(pt.Y)) End Sub End Class ' Form to test the virtualizer Private Class MyForm : Inherits Form Public Sub New(ByVal imageFileName As String) _imageFileName = imageFileName End Sub Private _imageViewer As ImageViewer Private _imageFileName As String Protected Overrides Sub OnLoad(ByVal e As EventArgs) ' Create a new image viewer with vertical layout Dim viewLayout As New ImageViewerVerticalViewLayout viewLayout.Columns = 1 Dim _imageViewer As New ImageViewer(viewLayout) _imageViewer.BackColor = Color.Bisque _imageViewer.Dock = DockStyle.Fill ' Add pan zoom interactive mode _imageViewer.InteractiveModes.Add(New ImageViewerPanZoomInteractiveMode()) Me.Controls.Add(_imageViewer) _imageViewer.BringToFront() ' Add empty items, each one with the correct size but with Using codecs As RasterCodecs = New RasterCodecs() Dim pageCount As Integer Dim pageSize As LeadSize Dim pageResolution As LeadSizeD Using info As CodecsImageInfo = codecs.GetInformation(_imageFileName, True) pageCount = info.TotalPages pageSize = LeadSize.Create(info.Width, info.Height) pageResolution = LeadSizeD.Create(info.XResolution, info.YResolution) End Using _imageViewer.BeginUpdate() Dim page As Integer = 1 Do While page <= pageCount ' no image data, the virtualizer will take care of loading the pages and rendering them Dim item As ImageViewerItem = New ImageViewerItem() item.ImageSize = pageSize item.Resolution = pageResolution _imageViewer.Items.Add(item) page += 1 Loop _imageViewer.EndUpdate() End Using ' Finally, set our virtualizer so it can start working _imageViewer.Virtualizer = New MyVirtualizer(_imageFileName) ' Run the demo, use pan (drag) and zoom (CTRL-CLICK and drag) to show the items ' Note how they get loaded when they are in view ' Change the number of MaximumItems to see its effect. MyBase.OnLoad(e) End Sub End Class
using Leadtools; using Leadtools.Controls; using Leadtools.Codecs; using Leadtools.Drawing; public void ImageViewerVirtualizer_Example() { string imageFileName = GetExampleImageFile(); // Create a new form to test out virtualizer MyForm form = new MyForm(imageFileName); form.ShowDialog(); } private static string GetExampleImageFile() { string imageFileName = Path.Combine(ImagesPath.Path, "ImageViewerVirtualizerExample.tif"); // Check if it is previously created, return it if (File.Exists(imageFileName)) return imageFileName; // Create an image that has many pages, we will use that for out testing const int pageCount = 100; const int imageWidth = 800; const int imageHeight = 800; var rc = new Rectangle(0, 0, imageWidth, imageHeight); using (RasterCodecs codecs = new RasterCodecs()) using (Font font = new Font(FontFamily.GenericMonospace, 80)) { for (int page = 1; page <= pageCount; page++) { using (RasterImage image = RasterImage.Create(imageWidth, imageHeight, 24, 96, RasterColor.FromKnownColor(RasterKnownColor.White))) { IntPtr hdc = RasterImagePainter.CreateLeadDC(image); using (Graphics graphics = Graphics.FromHdc(hdc)) { graphics.DrawRectangle(Pens.Blue, 0, 0, rc.Width - 1, rc.Height - 1); TextRenderer.DrawText(graphics, "Page " + page.ToString(), font, rc, Color.Red, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter); } RasterImagePainter.DeleteLeadDC(hdc); codecs.Save(image, imageFileName, RasterImageFormat.TifLzw, 0, 1, -1, -1, page == 1 ? CodecsSavePageMode.Overwrite : CodecsSavePageMode.Append); } } } return imageFileName; } // Custom Virtualizer public class MyVirtualizer : ImageViewerVirtualizer { private string _imageFileName; public MyVirtualizer(string imageFileName) : base() { _imageFileName = imageFileName; // We will 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 + 1, but it // could be anything in your application, for example // using ImageViewerItem.Tag to store the necessary data to identity this item int pageNumber = this.ImageViewer.Items.IndexOf(item) + 1; 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) + 1; System.Diagnostics.Debug.WriteLine("SaveItem for page {0} 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 place holder 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); } } // Form to test the virtualizer class MyForm : Form { public MyForm(string imageFileName) { _imageFileName = imageFileName; } private ImageViewer _imageViewer; private string _imageFileName; protected override void OnLoad(EventArgs e) { // Create a new image viewer with vertical layout _imageViewer = new ImageViewer(new ImageViewerVerticalViewLayout { Columns = 1 }); _imageViewer.BackColor = Color.Bisque; _imageViewer.Dock = DockStyle.Fill; // Add pan zoom interactive mode _imageViewer.InteractiveModes.Add(new ImageViewerPanZoomInteractiveMode()); this.Controls.Add(_imageViewer); _imageViewer.BringToFront(); // Add empty items, each one with the correct size but with 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++) { // no image data, the virtualizer will take care of loading the pages and rendering them ImageViewerItem item = new ImageViewerItem(); item.ImageSize = pageSize; item.Resolution = pageResolution; _imageViewer.Items.Add(item); } _imageViewer.EndUpdate(); } // Finally, set our virtualizer so it can start working _imageViewer.Virtualizer = new MyVirtualizer(_imageFileName); // Run the demo, use pan (drag) and zoom (CTRL-CLICK and drag) to show the items // Note how they get loaded when they are in view // Change the number of MaximumItems to see its effect. base.OnLoad(e); } }