Displaying multi-page tiff files using the ImageBox control and C#
A brief article showing how to display individual pages from a multi-page tiff file in the ImageBox control.
I suspect the titles of these last pair of articles are a touch
misleading as they talk about extended zoom operations without
actually describing a zoom process (as this is already part of
other ImageBox articles. Unfortunately I can't really think
of better titles and the theory is generic enough to be applied
to any type of zooming, not just the
My previous article touched on zooming in a
ScrollableControl while keeping the content correctly aligned
to a fixed point, usually the mouse position prior to the zoom.
This article expands on that with another new feature in the
ImageBox update, zooming to a given region. You've
probably seen this behaviour in other paint programs, where you
select a Zoom tool, draw a rectangle, and the document is
automatically zoomed to fit.
Again, this is actually quite simple to achieve and this blog
post is pretty much just a reference to me of how I did this so
I don't forget next time I want something similar! We'll start
off by quickly describing a method that I forgot to include in
the previous post -
CenterAt. This simple method centers the
view port on the given document location. This use useful for
when you want to use
ScrollTo without providing a relative
offset... or for centering the display on a selection rectangle.
Very straightforward, it simply calls
ScrollTo, using the
center of the control as the offset. Now for the actual
This accepts a
RectangleF structure (you could use a
Rectangle, but then if you attempt to draw selection regions
on a zoomed out document, rounding from
render your selections useless), and it then calculates a new
zoom factor and offset.
CenterAtmethod to center the view port on the center of the source region
In the actual
ImageBox control, a new
SelectionMode has been
Zoom. As the name somewhat logically suggests, when
this mode is active, after the user draws a selection rectangle,
the control then zooms to match the rectangle they have drawn.
This updated mode is called from the
OnSelected method, as
ImageBox.SelectionDeadZone field is a constant
currently set to 5, and basically ensures the user selects a
valid rectangle first - when I was testing the first iteration
of this code, having the mouse wobble as you clicked the control
was enough to generate a 1x1 rectangle, definitely not a good
The only disadvantage is this functionality only lends itself to zooming it, and not out.
Like what you're reading? Perhaps you like to buy us a coffee?