Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Poor Man's TIFF Viewer

0.00/5 (No votes)
25 Oct 2015 1  
A Single-DLL, multi-page TIFF .NET viewer control with panning and thumbnails support

Introduction

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 application with limited TIFF viewing needs.

Background

I occasionally need for 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 32bit/64bit 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:

The following project displays multipage TIFFs but is missing thumbnails support:

http://www.codeproject.com/Articles/31247/Multipage-TIF-Viewer

The following project does have thumbnails support, but it is for ASP.NET:

http://www.codeproject.com/Articles/64146/ASP-NET-Multipage-TIFF-Viewer-with-Thumbnails

So I decided to come up with my own control.

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

Three classes are used as viewers:

PageControl

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

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

ThumbnailsControl

With a few minor modifications, standart 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, select the page of TIFF file to display.

Presenter

TiffViewer is the presenter and the control itself. It controls the interaction between the model and views describeed 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...', 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here