Y'herd thisun? 

“! could either watch it happen, or be part of it.”
-Elon Musk

Your own image control and App part 25

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.


25) Saving to image files

GDI+. Did I mention that it's one of the best excuses for moving to .Net? With it, you can throw away your old 3rd party ocxes and dlls and easily save your Image objects out to files in the most commonly used formats using some of the extra features specific to each type. (There are limits, of course, and they aren't always obvious. For instance you can create new tiff files with multiple pages giving each page a different resolution but even though GDI+ has options for color reduction those options don't work. To create a 1BitPerPixel image for the file you have to P/Invoke GDI32).

For our application we're going to support exporting to JPEG with resizing and definable compression. If you want to offer other types, the basic code is very similar and you can find examples of each format's optional intricacies in the MSDN documentation.

The coolest way to offer user-defined file saving is to put the options right on the Save dialog. This has been an option for years via message hooking and it's got a tiny bit eaiser with .Net (Martin Perry recently revisited the technique in the MSDN SmartClient devcenter). Thing is, it's still not simple and it's definitely not bulletproof. I brought it up for the coolness factor but I advise that you use a regular form set up with DialogResults instead.

Here's mine, to give you the idea:

Add a form to the app project, set its Name to "frmSaveJPEG", its BorderStyle to "FixedToolWindow" and its Text to "JPEG save options.

Draw two buttons on the lower right "butSave" and "butCancel". Set butSave's DialogResult property to "OK" and butCancel's to "Cancel". Double click on butCancel to add the close, and while you're in codeview force the default DialogResult to Cancel in the form Closing event (takes care of the user clicking the form "X" button.


Private Sub butCancel_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles butCancel.Click
End Sub
Private Sub frmSaveJPEGOptions_Closing(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles MyBase.Closing
If Me.DialogResult = DialogResult.None Then
Me.DialogResult = DialogResult.Cancel
End If
End Sub


Back to the designer. Drop four RadioButtons to the form, make them wide to accommodate dynamic text that will tell the image sizes and set their main properties:

  • Name="rbSaveCurrent" Text="current size"
  • Name="rbSaveSpecific" Text="specific size"
  • Name="rbSaveAspectPercent" Text="aspect correct percent"
  • Name="rbSaveAspectExact" Text="aspect correct by max"

(The Text properties are just for designtime, at runtime the texts will be overwritten).

Add a Trackbar (slider) to the bottom of the form. Name it "trackQuality" and set its limits from 1 to 100. Put a label near it to tell the users that the trackbar is for adjusting image quality.

Before you start adding the textboxes, think about all the traditional hoops you're going to have to jump through to validate their texts. You only want the user to be able to enter whole numbers, the numbers can only be positive, the numbers have to be within certain specific ranges as determined by the image and if the numbers don't validate then you want to disable the form's Save button. You know the drill, GotFocus, LostFocus, KeyDown and TextChanged event minutia, made more annoying in this case because we have so many mutually exclusive options to check.

I've got a better way. Instead of allowing anything to be stuck in the textboxes then Validating then enabling/disabling the Save button, why not keep the Save button enabled all the time and not let the user enter invalid values in the first place? All it takes is a usercontrol.

Next: An integer-only textbox

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