Tuesday, January 06, 2009
Home
.Net
Return of the Repeater
Complete image control & app
Part 2 - Starting the control
Part 3 - Loading images
Part 4 - Custom exceptions
Part 5 - Fax/Multipage tifs
Part 6 - Custom events
Part 7 - Selecting fax pages
Part 8 - Rotating displays
Part 9 - The power of GDI+
Part 10 - Zooming
Part 11 - .Net's fatal exception
Part 12 - Fix the squishies
Part 13 - Zoom to fit
Part 14 - You beat the pros
Part 15 - Cropping
Part 16 - Bonus: StickyMouse
Part 17 - Finishing touches?
Part 18 - Make the application
Part 19 - Adding viewports
Part 20 - A better toolbar
Part 21 - Connect the toolbar
Part 22 - Adding ImageEditors
Part 23 - Toolbar ZoomCombo
Part 24 - VB3 style ease
Part 25 - Saving images to files
Part 26 - Integer-only textBox
Part 27 - Passing save settings
Part 28 - The last exception
Part 29 - Menus offer more
Part 30 - Book, app & source
Couple of CS Snippets
XML processing quickies
File extension extensions
McAfee.Not
cs IntBox
Floating Holidays
Code snippets
Flexible sorts
Converted deserializations
Autodeploy not found
Autodeploy just stops
VB must be killed
Media file attributes
Fastest file searches
Webservicing custom objects
Aspect correct resize
Funky thumbnails
Wide interval timing
VB2005 or bust?
Recurring events
Single instances
Proper proper casing
Simple address formating
Combo filling reminder
Easy gradient forms
Grrrr on interops?
Winform memory usage
Windows service your ISP
Pretty up VS code printing
Remote configs with no BS
GDI+ printing cd cases
Your own flat combo
Where's the splitter?
Full autoemail
ReessppoonncceeWwrriittee
Kill the back cache
Color to hex
Source to web
Recode without recompile
Prop snippet for VS2008
Database tricks

Your own image control

Originally published December 2002 on DoItIn.net 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
        Get
            If Not m_OriginalImage Is Nothing Then
                Try
                    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


Print  

pagecomment
  Add Comment



Submit Comment
  View Ratings
50.00%0
40.00%0
30.00%0
20.00%0
10.00%0

Number of Comments 0 , Average of Ratings
  View Comments
No comment.


Privacy Statement  |  Terms Of Use
Copyright 2008 by Robert C. Smith