Engage on Facebook Engage on Twitter Engage on LinkedIn Engage on GitHub components notes mobile card heart 2 infinite mirror 2 pricing support

How To: Get the size of an image (remote or local)

By Brian Dukes

I am often left to the task of processing images to display on a web page.  These images are rarely formatted beforehand to look great on the page (and I'd prefer to not force the user to have to format them, even if I could).  One of the main problems with this is image size.  If we are dealing with any sort of tight layout, images that aren't the standard size can ruin how a page looks.

So, ideally, we need to find the size of the image, and then decide how to scale the image so that it fits just right.  While it would be nice to get ImageHeight and ImageWidth properties off of the Image control, what we really need to do is pull down the image to the server (if it isn't there already), and from there we can get a better look at it.

To get the image to the server and into an Image, use:


using (System.Net.WebClient client = new System.Net.WebClient())

using (System.Drawing.Image resizableImage = System.Drawing.Image.FromStream(client.OpenRead(imageUri)))

If you already have the image available locally, you can use Image.FromFile instead of FromStream. From here, we have access to resizableImage.Size, .Height, and .Width.



Planning a DNN upgrade? Download our guide