Click here to Skip to main content
12,890,388 members (57,727 online)
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 24 May 2007

ASP.NET VwdCms.ImageZoom Control - JavaScript to Resize/Scale Images

, 24 May 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
VwdCms.ImageZoom is a server control that provides zoom in and out and best-fit image resizing. The control is very simple and is a good example of how to manipulate the size of images with JavaScript

Try out the VwdCms.ImageZoom control online: ImageZoom Control Online Demo

Screenshot - VwdCmsImageZoom1.gif


Level: Beginner JavaScript & DHTML

I actually built the VwdCms.ImageZoom control just as a sample control to use to test another project that I am working on - creating Toolbars and specifically sharing Toolbars between multiple controls on a page. This version of the ImageZoom does not have its own toolbar built into it - the toolbar that you see is just HTML on the sample Web Form.

I will be posting another article shortly on Toolbars which will contain a modified (enhanced) version of the VwdCms.ImageZoom as an implementation example. Rather than discussing the ImageZoom in that article, or not discussing it at all, I have decided to present it here first. I think that the JavaScript code in this control is interesting because of its simplicity. I hope that the code samples and discussion will be useful for anyone trying to manipulate or resize images / img elements on the client.

Using the Control

To add the VwdCms.ImageZoom control to your project, just follow these steps:

Step 1

Copy these files into your project directory:

  1. /App_Code/ImageZoom.cs
  2. images/zoomin.gif
  3. images/zoomout.gif
  4. images/actualsize.gif
  5. images/bestfit.gif

Step 2

Update your web.config file's Controls section: (inside the system.web section)

        <add tagPrefix="VwdCms" namespace="VwdCms"/>

Step 3

Add a VwdCms.ImageZoom control to your Web Form:

<VwdCms:ImageZoom runat="server" ID="izImgZoom"
style="width:450px;height:400px;overflow:auto;" />

Step 4

Create a toolbar similar to the one in the sample project. Note: I will be posting an updated version of the VwdCms.ImageZoom control soon that has a built-in Toolbar and will support "Toolbar Sharing".

All of the JavaScript code is included in the C# class so there is no need to include a script reference. The element IDs that appear in the code sample below are automatically generated by the C# class, so you do not need to worry about setting them or hard-coding them.

I have verified that this code works in Internet Explorer 7 and Firefox 2.0.

Key Concepts

When you want to modify the dimensions of an image that will be rendered on a Web page, the key to making them look good is to only set one of the dimensions. By setting the width only, you are allowing the browser to scale the image and determine the height - the result is a much more presentable looking enlargement or reduction.

The same holds true for zooming in or out on images using JavaScript - just set one dimension, I prefer to set the width property. Another key point in zooming in and out is getting the image to return back to its original size. You don't need to do any fancy coding or store the original width in variables, all you do is remove the width attribute from the image (img element) using the standard DOM removeAttribute('width') method. After you remove the width attribute, the browser will render the image at its original size.

The 'Best Fit' code is interesting because as it turned out, it was not really that complicated. To do a 'Best Fit' on an image, you need to know the 'aspect ratio' (ar in the code) for the image and you need to know the width (clientWidth, cw in the code) and height (clientHeight, ch in the code) of the box that you want to fit the image into.

Aspect Ratio: ar = img.width / img.height;

An aspect ratio greater than 1.0 means that the image's width is greater than its height. Alternatively, an aspect ratio less than 1.0 means that the image's height is greater than its width.

Using the information above, we need to set the width of the image for the two situations.

Aspect Ratio >= 1.0 : Set the width of the image equal to the width of the box that you want to fit it into, img.width = cw;.

Aspect Ratio < 1.0 : Compute the width of the image using width = ch * ar;, this is just a rearrangement of the Aspect Ratio formula above except that in place of the img.width, I have substituted in the 'box height' ch and the net result of the equation is a width scaled based on the height of the box I want to fit the image into.

JavaScript Code

The neat part of this control is really the JavaScript because it is so simple...

function setImage(src)
        var img = document.getElementById('imgMain');

        // make sure image is loaded using its
        // original dimensions the first time

        // change the URL for the image element
        img.src = src;
        alert('setImage error: ' + e.message);

function zoomIn()
        var img = document.getElementById('imgMain');

        // zoom in by 25%
        img.width = img.width * 1.25;
        alert('zoomIn error: ' + e.message);

function zoomOut()
        var img = document.getElementById('imgMain');

        // zoom out by 25%
        img.width = img.width * 0.75;
        alert('zoomOut error: ' + e.message);

function actualSize()
        var img = document.getElementById('imgMain');

        // return the image back to its
        // original dimensions
        alert('actualSize error: ' + e.message);

function bestFit()
        var iz = document.getElementById('izImgZoom');
        var img = document.getElementById('imgMain');
        var cw = iz.clientWidth;

        // sometimes the clientWidth is 0 in IE,
        // so look at the parent elements until
        // a valid clientWidth is found
        while ( cw == 0 )
            iz = iz.parentElement;
            cw = iz.clientWidth;
        var ch = iz.clientHeight;

        // return the image back to its
        // original dimensions

        // calculate the aspect ratio
        var ar = img.width / img.height;

        if ( ar >= 1.0 )
            // easy, set the image width to
            // the 'box width' minus 2 pixes for
            // the border of the image
            img.width = cw-2;
            // compute the image width using
            // the 'box height' and 'aspect ratio'
            // minus 2 pixes for the border of the image
            img.width = Math.floor(ch * ar)-2;
        alert('bestFit error: ' + e.message);


  • 24th May, 2007 - Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jeff Bazinet
Web Developer
United States United States
VWD-CMS is an ASP.NET content management system designed and developed by Jeff Bazinet.

While VWD-CMS has been developed entirely in Visual Web Developer, it will power any ASP.NET 2.0 website.

VWD-CMS is a file based content management system that has been built with developers in mind -

one of the primary design requirements is that the CMS does not constrain or limit a developer.

Try the online demo and learn more about VWD-CMS at

You may also be interested in...

Comments and Discussions

GeneralGreat work indeed! Pin
Jigneshhrathod24-May-12 2:46
memberJigneshhrathod24-May-12 2:46 
GeneralMy vote of 5 Pin
manoj kumar choubey16-Feb-12 0:23
membermanoj kumar choubey16-Feb-12 0:23 
GeneralProblem with the demo code Pin
weberdavid21-Oct-10 23:13
memberweberdavid21-Oct-10 23:13 
GeneralHelp about width of object Pin
F2F8-Sep-07 17:50
memberF2F8-Sep-07 17:50 
GeneralSmall improvement Pin
tfranch30-Aug-07 1:17
membertfranch30-Aug-07 1:17 
Question??? Pin
merlin98124-May-07 6:59
membermerlin98124-May-07 6:59 
AnswerRe: ??? Pin
VWD-CMS24-May-07 7:45
memberVWD-CMS24-May-07 7:45 
GeneralExcellent Pin
merlin98124-May-07 3:08
membermerlin98124-May-07 3:08 
AnswerRe: Excellent Pin
Uwe Keim24-May-07 4:07
sitebuilderUwe Keim24-May-07 4:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 24 May 2007
Article Copyright 2007 by Jeff Bazinet
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid