Blue Theme Orange Theme Green Theme Red Theme
 
Mindcracker MVP Summit 2012
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Chart
Search :       Advanced Search »
Home » JavaScript » Get the Width and Height of an Image using Javascript

Get the Width and Height of an Image using Javascript

Get the Width and Height of an Image using JavaScript easily using the width/height property.

Page Views : 46841
Downloads : 76
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
getimageattr.zip
 
 
Mindcracker MVP Summit 2012
Become a Sponsor
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

In some scenario while working with images, we may in need to know the width and height of an image, which may be loaded dynamically or have to access an image already loaded whose size attributes may be unknown.

 

We can programmatically find/get the Width and Height of an Image using JavaScript easily using the "width" and "height" property.

 

We can access the image using it's id attribute. If the id attribute is not added to the image tag, first we have to add 'id' attribute to the image. Then we can access the image like this:

 

var theImg = document.getElementById('our_image_id');

 

Now we can refer the image using 'theImg' variable.

 

To get the Width of the image, we can just refer as theImg.width. like that we can do for getting the height.

 

For Example:

 

<HTML>

    <HEAD>

        <TITLE></TITLE>

        <script language="javascript">

        function getW()

        {

            var theImg = document.getElementById('testimg');

            alert(theImg.width);

        }

        function getH()

        {

            var theImg = document.getElementById('testimg');

            alert(theImg.height);

        }

        </script>

    </HEAD>

 

    <BODY>

        <img id="testimg" src = "palette.gif" border="0"/>

        <input type="button" value="get Width" onclick="getW()"/>

        <input type="button" value="get Height" onclick="getH()"/>

    </BODY>

</HTML>

 

Output:

 

If you click on the "get Width" button, you will see the output like this:

 

 

Figure 1: Messagebox represent the width of the image.

 

If you click on the "get Height" button, you will see the output like this:

 

 

Figure 2: Messagebox represent the height of the image.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Kumar S
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
how to get height and width of an image using javaScript by Binod On June 4, 2009

I was searching a solution to get height and widht of an image using javascript. I got many but all those solution only worked when image present in browser cache.

Finally I got one solution to get image height and width even image does not exist in browser cache. Just I want to share all of you.

[code]
<script type="text/javascript" language="javascript1.2">

var imgHeight;
var imgWidth;

function findHHandWW() {
imgHeight = this.width;imgWidth = this.width;return true;
}

function showImage(imgPath) {
var myImage = new Image();
myImage.name = imgPath;
myImage.onload = findHHandWW;
myImage.src = imgPath;
}
</script>

[code]

Thanks,

Binod Suman

http://binodsuman.blogspot.com/2009/06/how-to-get-height-and-widht-of-image.html

Reply | Email | Modify 
Get the Width and Height of an Image using Javascript by Mahesh On March 30, 2010
Thanks.

Is this possible to get more than 1 image dimensions and display them onmouseover event.

like,

<HTML>

    <HEAD>

        <TITLE></TITLE>

    </HEAD>

 

    <BODY>

        <img id="f1" src = "1.jpg" border="0"/>

<p>Test</p>

        <img id="f2" src = "2.jpg" border="0"/>

<p>Test</p>

        <img id="f3" src = "3.jpg" border="0"/>

<p>Test</p>

<p>Test</p>

<p>Test</p>

<p>Test</p>

        <img id="f4" src = "4.jpg" border="0"/>

<p>Test</p>

    </BODY>

</HTML>

Reply | Email | Modify 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.