smithvoice.com
 Y'herd thisun? 

“International competition rather than international cooperation motivated states to open terrestrial frontiers for centuries, and that motivation will have to be harnessed again for our species to permanently occupy other worlds of the solar system.”

from Reopening the Space Frontier by John Hickman

Your own image control and App part 13

TaggedCoding, VB, Imaging

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


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 smithvoice.com