Image Information - Size, Width and Height - May 2010

If you download this tip please consider making a modest donation.

A couple years ago I used Google to search for code that would provide image size information. I encapsulated that code in module cImageInfo. At the end of last year I revised the code and created module cImageSize. This month's tip documents and distributes that code.

The ImageSize Application

I created a VS 2008, Asp.Net, VB.Net web solution project which is called ImageSize. It demonstrates retrieving image information from graphics files.


This image file is 13,771 bytes. The image has a width of 216 pixels and height of 288 pixels.

Image - The photo of Birthday Boy is distributed with the source code in AppPath.

New Image - Click the Browse button to find a graphics file. The Asp.Net FileUpload control is used.

Click to Get Image Information - Click this button to upload the image, display image information and display the image.

Size, Width, Height - The file size [in bytes], the width and height [in pixels] are reported.

Properties and Methods

Class modules cImageInfo and cImageSize accept a number of properties and have several methods:

Method/Property Description Example
cImageInfo    
.Load(sFileName) Load the filename into a Bitmap,
.bFailed is True if it fails
Dim o as New cImageInfo
o.Load("BirthdayBoy.jpg")
.IsGraphic(sFileName) Returns True if is a graphic.
Attempts a .Load and if it succeeds returns True.
Uses Try, Catch to set .bFailed.
If o.IsGraphic("BirthdayBoy.jpg") Then
.Width Return the width. Call .Load first Dim iWidth As Integer = .Width
.Height Return the height. Call .Load first. Dim iHeight As Integer = .Height
.Format Returns the graphic's format. Call .Load first
For example: BMP, JPEG, Icon, etc.
Dim sFormat As String = .Format
cImageSize    
.GetImageSize(sFileName) Uses:
Dim oImage As System.Drawing.Image
oImage = System.Drawing.Image.FromFile(sFileName)
Dim o as New cImageSize
o.GetImageSize("BirthdayBoy.jpg")
.lSize The size of the graphic in bytes. Call .GetImageSize first Dim lSize As Long = .lSize
.Width Return the width. Call .GetImageSize first Dim iWidth As Integer = .Width
.Height Return the height. Call .GetImageSize first. Dim iHeight As Integer = .Height

Download VB.Net Code

Download the source code by clicking on the zipped file name. You will need to run the application in a folder on your computer that allows files to be written. I was not able to run the application from a Vista Desktop. I moved the unzipped folder to the root directory and it ran OK.

Download - Source Zipped File Size
VS2008 VB Solution ImageSize.zip 60K

Previous Tip   Tip Index    Next Tip

Home

Updated 2010/05/27