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.


13) Zooming to fit the control

It's not in the spec, but there's an obvious feature just waiting to be asked for: ZoomToFit. This is easily added as a new public method. The fun bit of logic is that we have to check zoom values twice to cover both the image's dimension ratio and the UC's dimension ratio:

Public Sub ZoomToFit()
        'calculate the ratio between the current size of the UC and
        'the longest dimension of the image
        'set the zoom percent to the result

        Dim ratio As Double
        Dim finalZoom As Double
        Dim tmpImage As Image

        If Not m_ImagePages Is Nothing AndAlso m_ImagePages.Length > 0 Then
            tmpImage = m_ImagePages(m_CurrentPageNumber).WorkingImage

            If tmpImage.Width > _
                tmpImage.Height Then
                ratio = Me.Width / tmpImage.Width
                finalZoom = ratio * 100

                'make sure it fits current control dimensions
                If Me.Height < tmpImage.Height * finalZoom / 100 Then
                    ratio = Me.Height / tmpImage.Height
                    finalZoom = ratio * 100

                End If

            Else
                ratio = Me.Height / tmpImage.Height
                finalZoom = ratio * 100

                'make sure it fits current control dimensions
                If Me.Width < tmpImage.Width * finalZoom / 100 Then
                    ratio = Me.Width / tmpImage.Width
                    finalZoom = ratio * 100

                End If

            End If

            m_ImagePages(m_CurrentPageNumber).CurrentZoom = CInt(Math.Floor(finalZoom))
            UpdateZoomDisplay()

        End If

        tmpImage = Nothing

    End Sub

After all of our talk on Ints, the use of Doubles might seem odd but in the case of calculating a useful ratio between the UC and image dimensions we need the extra precision. Because of Integer rounding and the use of a whole percentage for the zooms, the displayed image may not exactly fit to the extreme edges of the control, if you take out the Math.Floor you may get a more snug fit but there is also the chance that a positive rounding will exceed the control limits and draw scrollbars. (If that really bothers your users then you'll have to widen the ZoomPercent's precision, just keep in mind that you might never get it 100% exact for all images because in the end the picturebox/UC's size is set with a whole integer)

It's been a long time coming, and here it finally is: "Rebuild the solution and Save". With all we just hacked and slashed I truly hope you didn't wait for me to tell you to do that <g>

Now head over to the testHarness one last time in this section. Go into the ZoomChanged event and add a couple of messageboxes to test the new properties of the eventargs. In a real app these booleans will be best linked directly to the Enabled property of zoom buttons and/or menu items, but this is fine for us now:

Private Sub SvImageEditor1_ZoomChanged(ByVal sender As Object, _
        ByVal e As Smithvoice.svImageZoomChangeEventArgs) _
        Handles SvImageEditor1.ZoomChanged

        lblZoomInfo.Text = "Zoom: " & e.CurrentZoom & "%"

If e.MinLimitReached Then MsgBox("Min limit reached, disable zoom out")
        If e.MaxLimitReached Then MsgBox("Max limit reached, disable zoom in")

    End Sub

Have fun with it. Bang on it. Try to set zooms into the thousands and make sure it won't let you. Use the demo fax tif file and set different rotations and zooms to different pages. So far so good?

Now set your display to 32bit then start the testHarness, load an image and zoom in as far as you can, then drop your color depth to 16 and see what happens... should it now allow you to zoom in more? Hmmm. Restart the testHarness so it locks in on the 16bit, load an image and zoom in all the way, then up the display to 32bit .. again that stinking crash.

If your control is going to be part of a mission critical app or one that also hits a database and could corrupt data with a crash then you can add a handler for the Win32.SystemChanging event and watch for changes in the Color Category. If these events come up then you'll have to reevaluate all of the CurrentZooms to see if they need to be changed. Personally, from real release experience and with ADO.Net doing its work without holding the pipes open I think it's usually not worth the code. I know, we've gone deep into minutia already but what we've done needed to be done while getting into support for this truly "exceptional" case is, in my experience, just plain overkill.

If that sounds like a cop-out or makes you feel bad, then let this next short section ease your worried mind...

Next: Compare your features to professional tools

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