developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See the Winners!


Developer Jobs

Be a Commerce Partner
Data Center Solutions
Career Education
Web Hosting Directory
Online Education
Rackmount LCD Monitor
Baby Photo Contest
Calling Cards
Laptops
Holiday Gift Ideas
Home Improvement
GPS Devices
GPS
Promotional Pens
Promos and Premiums

 


Related Article -
File Uploading Using ASP.NET
Using ASP.NET To Send Email
ASP.NET Tip: Creating Scrollable Micro Windows
Taking the Visual out of Visual Basic
Accessing Files and Directories
Developer News -
SaaS Tool Offers Custom Database Development    May 9, 2008
Microsoft’s Automated Agent: Can We Talk?    May 7, 2008
Borland Finally Sells CodeGear    May 7, 2008
Red Hat Heads For The JON 2.0    May 7, 2008
Free Tech Newsletter -

Project Management Guide: Developing a Web Site. Best Practices, Tips and Strategies. Download Exclusive eBook Now.

Generating Thumbnails on the Fly Using ASP.NET!
By Karl Moore

If you've ever attempted to create image thumbnails for your site, you'll know it's a tiresome task. You either do it manually, or use an inflexible system such as the FrontPage thumbnail feature.

However, as you've seen in previous tips, ASP.NET gives us tremendous control over how our images work. As such, we should be able to generate thumbnails on the fly... and this snippet will enable you to do just that.

Create a new Web form (thumbnail.aspx) and add the following code in listing 1 behind the page Load event. (Listing one is at the bottom of the article)

If you follow the code in Listing 1 code through, you should see what happens. The filename is retrieved from the query string and the image loaded into memory. The new width is then taken and a proportionate height calculated (an important usability feat that few demonstration code snippets take into account). Then, a newly sized thumbnail is generated and sent straight back down to the client.

After compiling, you'd call this page by typing something like the following URL into your browser: http://localhost/WebAppFolderName/thumbnail.aspx?filename=myfile.gif&width=100. You'd use this URL as the image source for, say, an Image control.

Don't forget: after you've generated the thumbnail, you can still dynamically edit your image. Using the last tip, for example, you could add a little copyright notice next to each thumbnail. Powerful stuff, this imaging lark.

Figure: My company logo, resized to perfection

Listing 1:

' Initialize objects
Dim objImage, objThumbnail As System.Drawing.Image
Dim strServerPath, strFilename As String
Dim shtWidth, shtHeight As Short     
' Get image folder path on server - use "\" string if root
strServerPath = Server.MapPath("WebAppImageFolder\")
' Retrieve name of file to resize from query string
strFilename = strServerPath & Request.QueryString("filename")
' Retrieve file, or error.gif if not available
Try
    objImage = objImage.FromFile(strFilename)
Catch
    objImage = objImage.FromFile(strServerPath & "error.gif")
End Try
' Retrieve width from query string
If Request.QueryString("width") = Nothing Then
    shtWidth = objImage.Width
ElseIf Request.QueryString("width") < 1 Then
    shtWidth = 100
Else
    shtWidth = Request.QueryString("width")
End If
' Work out a proportionate height from width
shtHeight = objImage.Height / (objImage.Width / shtWidth)
' Create thumbnail
objThumbnail = objImage.GetThumbnailImage(shtWidth, _
  shtHeight, Nothing, System.IntPtr.Zero)
' Send down to client
Response.ContentType = "image/jpeg"
objThumbnail.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)
' Tidy up
objImage.Dispose()
objThumbnail.Dispose()

About the Author

Karl Moore (MCSD, MVP) is an experience author living in Yorkshire, England. He is author of numerous technology books, including the new Ultimate VB .NET and ASP.NET Code Book (ISBN 1-59059-106-2), plus regularly features at industry conferences and on BBC radio. Moore also runs his own creative consultancy, White Cliff Computing Ltd. Visit his official Web site at www.karlmoore.com.

# # #


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


ASP & ASP.NET Archives

Work With InterSystems. Not Separate Systems. Rapidly develop and deploy connectable applications.
Whitepaper: Enterprise Information Integration--Deployment Best Practices for Low-Cost Implementation
Developing Intelligent Communications? Visit the Avaya DevConnect Center on DevX.
Five Trends for Application Development. Download Your Complimentary Report. Exclusive. Act Now.
Whitepaper: XML Processing in Applications--Take the Next Step



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES