For an image file VB.NET can obtain the width and height of the image using the System.Drawing width and height properties.
Let us assume that our file image is /images/kennet_avon_canal.jpg and that we are going to illustrate the information we have found by using a label control lblImageInfo.
It is easier to import the drawing namespace and to use the shortened forms for referencing the functions.
Imports System.Drawing
The control for the label lblImageInfo is defined as
Protected WithEvents lblImageInfo As System.Web.UI.WebControls.Label
We will also define the parameters which we will be using
Dim imgImage As Bitmap
Dim imgWidth As Integer
Dim imgHeight As Integer
Dim strImageURL as String
To begin assign the image URL to a variable strimageURL.
strImageURL = “/images/kennet_avon_canal.jpg”
And assign our discovered values, after creating imgImage as a new Bitmap.
imgImage = New Bitmap(Server.MapPath(strImageURL))
imgWidth = imgImage.Width
imgHeight = imgImage.Height
Finally we will set the discovered width and height parameters of the image, plus the image itself, as the text parameter of our label control.
lblImageInfo.Text = “<div><img height= src=””” & imgImage & “”” width=””” & imgWidth & “”” height= “”” & imgHeight&”””></div>”
lblImageInfo.Text += “<div>Width = ” & imgWidth& “></div>”
lblImageInfo.Text += “<div>Height = ” & imgHeight & “></div>”


