Simple TIFF Viewer: Basic .NET Solution for Viewing TIFF Files

Introduction

TIFF

TIFF is a widely used file format employed in numerous image manipulation applications including (but not limited to) scanning, faxing, and optical character recognition. It is a mature format for storing raster images, whose last major update was in 1992.

This article provides a ready-to-use TIFF viewer control based on. NET's already existing capabilities, thus its brevity. The control has limitations and is obviously no match for commercial TIFF viewers feature-wise or performance-wise, but it should be sufficient for applications with limited TIFF viewing needs.

Background

I occasionally need prototype applications containing TIFF viewers. Although. NET's GDI+ already contains the functionality to decode and manipulate TIFF files, expensive commercial TIFF viewers are used for displaying TIFF files. The ones who have experience with them would agree that it is a tedious task properly setting them up (packing up the necessary set of DLLs, including the licenses, taking care of 32-bit/64-bit compatibilities, etc.).

With this in mind, I thought a bare-bones TIFF viewer control depending on .NET alone would already be available somewhere. But the ones I could find did not meet my needs. One I could display multipage TIFFs but was missing thumbnail support, another one did have thumbnail support, but it was for ASP.NET. So I decided to come up with my own control.

Control Basics

Control Basics

The control loosely follows the Model-View-Presenter (MVP) Pattern, so I will breakdown the control's classes according to their pattern roles:

Model

TiffImage class constitutes the model part. It contains the actual TIFF image data, which is stored in a System.Drawing.Bitmap object. It also contains a cached working copy of the image having the currently displayed page with the current zoom ratio.

Viewers

Viewers

Three classes are used as viewers

PageControl

A PictureBox is used to display the current page of the TIFF file.

The only thing worth mentioning is the panning support. PictureBox's MouseDown(), MouseMove(), and MoveUp() methods are implemented to support image panning.

ThumbnailsControl

With a few minor modifications, standard ListView has everything we need to be used as a thumbnail viewer.

A TIFF file may contain hundreds of pages, so we would not want all of our thumbnails to be loaded at startup. Here, the listview's virtual mode comes to our rescue. In virtual mode, the ListView requests only the thumbnails that are about to be displayed.

I use the ListView in LargeImage mode. I bind to listview to an empty ImageCollection and then I populate it with the pages' thumbnails as needed. The icons representing the thumbnails are all set to 100x100, TiffImage.GetThumbnail() shifts the images so that they are centered in this 100x100 viewport.

ToolbarControl

The ToolbarControl has a ToolStrip control which contains buttons to zoom in/zoom out the image, and select the page of the TIFF file to display.

Presenter

TiffViewer is the presenter and the control itself. It controls the interaction between the model and the views described above.

Using the control

A single DLL, TiffViewerLib.dll is required for the control. Right-click on your Visual Studio Toolbox, select 'Choose Items...', select 'Browse...', and select 'TiffViewerLib.dll'. Drag the TiffViewer control from the toolbox to your form. Set the Path property of the control to the TIFF file you would like to view.


Recommended Free Ebook
Similar Articles