Y'herd thisun? 

“It is my understanding that NASA Headquarters recently lost hundreds of positions, and after those halls were emptied no one could remember what in the world all those people did.”

from Getting to Space by Dr. Jerry Pournelle

Your own image control and App part 5

TaggedCoding, VB, Imaging

Originally published December 2002 on using VB7.0/2002. Updated for VB7.1 February 2005

Links for compiled demo versions, all required resources and source code are included at the end of this article.

Plus, get the complete eBook in Adobe Acrobat 7 format ... all here.


5) "Fax images" and Multipage TIFFs

If you tested the image loading with a "fax image" *.tif file (or *.tiff" file) you probably got it to display... sort of. Faxes received by computers are traditionally held in TIF files, but TIFs aren't exactly Image files in the manner of *.bmp, *.gif and *.jpg/*.jpeg.

"TIF" stands for "Tagged Image File" (the optional extra "F" in the extension being for "Tagged Image File Format") and this type of file is more of an envelope for multiple raster images than a straight single-image data file. We once heard a young internet-aged developer wave off TIFs as being "animated gifs that don't animate" but they're quite a bit more than that.

The great power of TIFs is that, unlike animated gifs, each image in the file can have a different color depth, codec ("COmpresson/DECompression" algorithm), size and X and/or Y resolutions from the others in the file. With these extra abilities TIFs suit many purposes including passing around different resolution versions of the same image (similar to putting 16x16, 32x32 and 64x64 versions of an application's icon in a resource dll), sending ink-specific color separations to a professional printer for high quality print jobs, holding discrete layers of a composite image for advanced editing and, of course, for holding all the pages of a fax transmission in a single file.

To get at the collection of a TIF's images back in the VBCom days the path of least grief was to buy into a 3rd party tool such as LeadTools, but .Net makes working with TIFs a lot easier. Now, with our own GDI+ and System.Drawing code we can not only read the images but also add, delete and edit a file's internal image collection. In our case today, we're just going to read the count of images and display specified "pages" on-demand, but because TIFs can be used for so many applications you should sometime take a look at all of the MSDN documentation on the subject.

Even if you do have some fax TIFs lying around, let's use the same file for this part of the project. I sent myself a fax from Kinko's and received it using a GoldFax Server system. The file has four pages including a half-sheet cover page and three pages showing different graphics. I put the file in a zip so that it wouldn't automatically open in your computer's registered tif viewer.Click here to download the zip then unpack the file "smithvoice_fax_test.tif" on your machine.

Double click on the file to open it in your default viewer. If you're using the XP/2k3 viewer you will see a page picker at the bottom of the applet, use this to look at the pages (other viewers provide the option differently, see the help file for your particular viewer to learn how to get at the image pages).

Warning: Unlike the Wang and Kodak fax viewing applets that shipped with Win9x/NT/2k, the Microsoft Image and Fax Viewer is a silently destructive editor. If you use the Rotate or Flip options the app will automatically overwrite the source file with the rotation. If you accidentally do that, dump the image file and get a fresh copy out of the zip.

If you're wondering why fax images look "squished" in the UC hold the thought, we'll address the issue when we get to zooming.

The easy part of supporting multipage TIFFs is getting the frame count out to the dev and end-users.

Open the svImageEdit UC in codeview. To save yourself some dots in this and later work, add an "Imports System.Drawing" and "Imports System.Drawing.Imaging" to the top of the file. Next, add a new region between "Declarations" and "Public Methods", name the new region "Public Properties".

Technically speaking the individual images in a TIFF are called "Frames" but we'll expose them to our dev-users with the name "Pages" to better fit the end-user mindset. Click in the new region and add an integer typed ReadOnly property named "TotalPageCount". The code for this property will interrogate the backup copy of the loaded image using GetFrameCount and specifying that the detail we're interested in is the System.Drawing.Imaging.FrameDimension.Page dimension. This call returns an integer of the number of frames/pages in the file.

Note that some, but not all, non-paged file types accept this method and return a count of 1. The ones that fail are typically proprietary formats such as CompuServe/UNISYS patented gifs. If you're feeling like Supergeek go ahead and test every kind of image file you can find and have your load code read the file header to decide whether to request the page count, but a faster and just as effective way to do things is to just trap the error that such a file throws which is "Runtime.InteropServices.ExternalException" (message: "A generic error occurred in GDI+") and when we hit that we return a value of 1.

If you want to use a private variable to hold the page count and only set it during the image load, feel free to do so. Personally I've found no perceptible performance hit doing the query as needed and it cuts down the number of lines (fewer lines, fewer bugs).

#Region "Public Properties"
Public ReadOnly Property TotalPageCount() As Integer
 If Not m_OriginalImage Is Nothing Then
 Return m_OriginalImage.GetFrameCount(FrameDimension.Page)
 Catch ex As Runtime.InteropServices.ExternalException
 'some formats such as Gifs don't support
 'Page framedimensions.
 'we'll just eat this error
 Return 1
 Catch ex As Exception
 'ToDo: remove this catch-all for release
 Throw ex
 End Try
 End If
End Get
End Property
#End Region

Next: Custom events


Robert Smith
Kirkland, WA

added to smithvoice march 2005


jump to:

  • 1) The spec
  • 2) Setting up the workspace 
  • 3) Feature 1: Loading an image
  • 4) Custom Exceptions
  • 5) "Fax images" and Multipage TIFFs
  • 6) Custom events
  • 7) Selecting specific fax pages
  • 8) Feature 2: Rotating image displays
  • 9) The most useful tool in GDI+: DrawImage
  • 10) Feature 3: Zooming
  • 11) Handling the unhandleable exception
  • 12) Fixing the squish
  • 13) Zooming to fit the control
  • 14) You're already beating the Pros
  • 15) Feature 4: Cropping
  • 16) Bonus Feature: StickyMouse
  • 17a) Final Cleanup
  • 17b) Passing the current display image
  • 18) Making the application
  • 19) Source and result viewports
  • 20) A better toolbar
  • 21) Hooking the toolbar to the project
  • 22) Adding ImageEditors
  • 23) The toolbar ZoomCombo
  • 24) The final solution
  • 25) Saving to image files
  • 26) An integer-only textbox
  • 27) Passing save options between forms
  • 28) Dealing with that last exception
  • 29) Offer more options with menus
  • 30 The downloads and ebook

  • home     who is smith    contact smith     rss feed π
    Since 1997 a place for my stuff, and it if helps you too then all the better