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.


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

        Me.Close()

    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


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