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.
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.
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
Imageobject does offer a
FrameDimensionsListproperty 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.
To change the active page the
Image object represents, you can
SelectActiveFrame method, passing in a
object and the zero-based page index. Again, we can use the
FrameDimension.Page property, similar to the
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.
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
Refreshought to be sufficient.
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.
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.
- 2016-07-30 - First published
- 2020-11-21 - Updated formatting
Like what you're reading? Perhaps you like to buy us a coffee?
- Displaying multi-page tiff files using the ImageBox control and C#
- Adding drag handles to an ImageBox to allow resizing of selection regions
- ImageBox 18.104.22.168 update
- ImageBox and TabList update's - virtual mode, pixel grid, bug fixes and more!
- ImageBox update, version 22.214.171.124
- Zooming to fit a region in a ScrollableControl
- Zooming into a fixed point on a ScrollableControl
- Creating an image viewer in C# Part 5: Selecting part of an image
- Extending the ImageBox component to display the contents of a PDF file using C#
- Creating a scrollable and zoomable image viewer in C# Part 4
- Creating a scrollable and zoomable image viewer in C# Part 3
- Creating a scrollable and zoomable image viewer in C# Part 2
- Creating a scrollable and zoomable image viewer in C# Part 1