Initializes a new instance of ImageViewerVirtualizer.
This constructor initializes the members of ImageViewerVirtualizer as follows:
Member | Value |
---|---|
16 |
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(LEAD_VARS.ImagesDir, "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);
}
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images";
}