This content has moved - please find it at https://devblog.cyotek.com.

Although these pages remain accessible, some content may not display correctly in future as the new blog evolves.

Visit https://devblog.cyotek.com.

Displaying multi-page tiff files using the ImageBox control and C#

Earlier this week I received a support request from a user wanting to know if it was possible to display multi-page tiff files using the ImageBox control. As I haven't wrote anything about this control for a while, it seemed a good opportunity for a short blog post.

Viewing pages in a multi-page file

Getting the number of pages in a TIFF file

One you have obtained an Image instance containing your tiff graphic, you can use the GetFrameCount method in conjunction with a predefined FrameDimension object in order to determine how many pages there are in the image.

private int GetPageCount(Image image)
{
  FrameDimension dimension;

  dimension = FrameDimension.Page;

  return image.GetFrameCount(dimension);
}

I have tested this code on several images, and even types which don't support pages (such as standard bitmaps) have always return a valid value. However, I have no way of knowing if this will always be the case (I have experienced first hand differences in how GDI+ handles actions between different versions of Windows). The Image object does offer a FrameDimensionsList property which returns a list of GUID's for the dimensions supported by the image, so you can always check the contents of this property first if you want to be extra sure.

Selecting a page

To change the active page the Image object represents, you can its SelectActiveFrame method, passing in a FrameDimension object and the zero-based page index. Again, we can use the predefined FrameDimension.Page property, similar to the following

image.SelectActiveFrame(FrameDimension.Page, page - 1);

After which, we need to instruct our ImageBox control (or whatever control we have bound the image to) to repaint to pick up the new image data.

imageBox.Invalidate();

You don't need to reassign the image (which probably won't work anyway if the control does an equality check), simply instructing it to repaint via Invalidate or Refresh ought to be sufficient.

A sample multi-page tiff file

As multi-page tiffs aren't exactly common images to be found in plenty on the internet, I've prepared a sample image based on a Newton's Cradle animation from Wikipedia.

Download NewtonsCradle.tif (4MB)

Short and sweet

The sample application in action

That is all the information we need to create a viewer - you can download the project shown in the above animation from the links below.

Update History

  • 2016-07-30 - First published
  • 2020-11-21 - Updated formatting

Downloads

Filename Description Version Release Date
ImageBoxTiffViewer.zip
  • sha256: 52680d16d3825c2096f502526f0de41b2db2992679cfe8154ad3c9d7836b664c

Sample project for the displaying multi-page tiff files using the ImageBox control and C# blog post.

30/07/2016 Download

About The Author

Gravatar

The founder of Cyotek, Richard enjoys creating new blog content for the site. Much more though, he likes to develop programs, and can often found writing reams of code. A long term gamer, he has aspirations in one day creating an epic video game. Until that time, he is mostly content with adding new bugs to WebCopy and the other Cyotek products.

Leave a Comment

While we appreciate comments from our users, please follow our posting guidelines. Have you tried the Cyotek Forums for support from Cyotek and the community?

Styling with Markdown is supported

Comments

Gravatar

XiaoFei

# Reply

hi,this message come from China.I am a coder come from China.I am very happy to connteciton you.Your control is very nice. I hope more exchanges.