Measuring the Size of an Image with C#

It can be useful to be able to retrieve the Height and Width of an image file you have on disk. The following snippet shows you how to do that:
// Load the Image
using (System.Drawing.Image objImage = System.Drawing.Image.FromFile(@"C:\MyImage.gif"))
  // Display its Height and Width
  lblSize.Text = "Width: " + objImage.Width + "<br />Height: " + objImage.Height;

The code loads an image from disk using the Static method: FromFile. The Image object than exposes its Width and Height properties which are simply displayed on a label.

Where to Next?

Wonder where to go next? You can read existing comments below or you can post a comment yourself on this article .

Consider making a donation
Please consider making a donation using PayPal. Your donation helps me to pay the bills so I can keep running Imar.Spaanjaars.Com, providing fresh content as often as possible.

Feedback by Other Visitors of Imar.Spaanjaars.Com

On Monday, May 07, 2007 5:40:04 AM izzliya said:
can u tell me how to restrict the size of the loaded image. when user clicks the upload button it should check the file size and validate them. if it is greater than the restricted size it should give an error msg

thank you
On Monday, May 07, 2007 6:21:46 AM Imar Spaanjaars said:
Hi izzliya,

Take a look here:

It's written for .NET 2.0 but with a few modifications, it should also work in 1.x


On Thursday, August 09, 2007 6:33:35 AM meghana said:
i want to restrict the image height & width before uplaoding the image on server. Kindly help me for the same.
On Thursday, August 09, 2007 7:52:40 AM Imar Spaanjaars said:
Hi meghana,

Take a look here:


On Thursday, September 06, 2007 8:30:49 AM Miguel said:

anybody know of a static method to get an image's height/width, that is, without instantiating a System.Drawing.Image?
I would like to improve performance when this is done massively.

On Thursday, September 06, 2007 8:14:15 PM Imar Spaanjaars said:
Hi Miguel,

You can simply resue the same objImage variable to avoid multiple instances and minimize memory consumption. The file has to be read one way or the other to get at its dimensions....

On Wednesday, November 18, 2009 10:05:03 AM Andreas said:
Great help! thanks. I use it to resize the images without deforming them. Keeping the same width-height ratio.
Thanks again saved a lot of time.
On Wednesday, November 10, 2010 1:29:14 PM Rasmus Schultz said:
This approach is a little too simple, and should NOT be used on a live web-server - it will result in severe CPU and memory overhead, and memory fragmentation on your server, as every single image will be loaded into memory.

If you look in the comment in the following post, someone else has posted an example demonstrating how to read the image properties using a .NET API, without actually loading the pixels into memory:

If you're working with a limited set of image resources, you might want to also consider wrapping this in a function, and adding a dictionary so you can cache the results.
On Monday, December 13, 2010 7:28:13 AM Hanif said:
System.Drawing.Image objImage = System.Drawing.Image.FromFile("C:\MyImage.gif");
The above coding is working fine in local system but not error shows in server
On Monday, December 13, 2010 7:33:34 AM Imar Spaanjaars said:
Hi there,

Sounds like a permissions issue to me. Make sure the web server process is allowed to read that file.


On Thursday, March 10, 2011 4:05:35 PM Pinank said:
How to Find width and Height of image with ".dxf".
in 2008 with c#.
On Thursday, March 10, 2011 4:06:48 PM Imar Spaanjaars said:
Hi Pinank,

No idea; sorry.

On Saturday, April 23, 2011 11:38:35 PM Bharat Mittal said:
helllo sir, i want to know how to make measure the dimension
object via picture of object in c#(maens snap short of object & this snap short is measure the width,height & lenght of the object).

On Sunday, April 24, 2011 1:41:00 AM Imar Spaanjaars said:
Hi there,

The images i am talking about in this article are always in 2D, so there is no concept of length. If you're working with some 3D model, try posting your problem on a forum such as and be sure to provide a lot more information such as the code you're working with.


On Tuesday, April 26, 2011 2:51:36 PM someone said:
well, i just wanted to say thanks :)
On Friday, April 29, 2011 5:51:54 PM Mayank Mehta said:
hey... i have read few article here on application architecture and really liked it...but i would like to add a quick note to this code that u have give here...

if u dont dispose the objimage  then u r in real trouble....

i was using this in a fileupload ....and realize that i keep getting an error ....that file used by a process.... so that was it!!

On Saturday, April 30, 2011 3:04:10 AM Imar Spaanjaars said:
Hi Mayank,

Good point. I updated the source code.


On Friday, February 24, 2012 7:50:52 AM sharath said:
hey i want to check the  height f image coming from application
for ex: http://application/image/top.gif . i used the same method but it is showing error near the filepath can u help me out

Thanks in advance
On Monday, March 05, 2012 9:57:29 AM Imar Spaanjaars said:
Hi sharath,

You can't do that. This code requires a local file path.
You could issue an HTTP request to download the file and store it locally.


On Friday, October 19, 2012 8:09:50 PM James said:
Can you formulate an example for storing the HTTP request locally
On Friday, October 19, 2012 8:12:41 PM Imar Spaanjaars said:
Hi James,

You can't store the request locally, but you can store its response. Google for ".NET download image" and you'll find many useful results.


On Wednesday, November 21, 2012 10:29:12 AM Manjunath B Yattinagudd said:
Sir..Good one.
This is what I required in one requirement

Many Many Thanks to you

Talk Back! Comment on Imar.Spaanjaars.Com

I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.

When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.

If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the QuickDocId of the document.

For more information about the Talk Back feature, check out this news item.