Image Width, Height, Resolution, Pixel Depth

Image Information: For C# and VB NET Students


In the previous lessons, we've opened an image and got what type of image it was. In this lesson, we'll use C# and VB Net code to get an image's, width, height, resolution, and pixel depth. We'll put all the information we've gathered so far into our text box. Your code should look like this so far, in C#:

C# code showing how to get which type of image was opened from an OpenFileDialog control on a Windows Form

And this what your code should look like in VB Net:

VB Net code showing how to get which type of image was opened from an OpenFileDialog control on a Windows Form


Image Width and Height

Let's get the image width and height first. It's quite easy!

After your if statement, add these two lines in C#:

string imageWidth = img.Width.ToString();
string imageHeight = img.Height.ToString();

And these two in VB Net:

Dim imageWidth As String = img.Width.ToString()
Dim imageHeight As String = img.Height.ToString()

Incidentally, another way to get the width and height is with the SizeF Class. You'd do it like this in C#:

SizeF dims = img.PhysicalDimension;
string imageHeight = dims.Height.ToString();
string imageWidth = dims.Width.ToString();

And this in VB Net:

Dim dims As SizeF = img.PhysicalDimension
Dim imageHeight As String = dims.Height.ToString()
Dim imageWidth As String = dims.Width.ToString()

It's up to you which one you use.


Image Resolution

But let's get the image resolution and the image pixel depth. Add this line to your code in C#:

string imageResolution = img.HorizontalResolution.ToString() + " dpi";

And this in VB Net:

Dim imageResolution As String = img.HorizontalResolution.ToString() & "dpi"

After the name of your image, img for us, type a dot. You can then use HorizontalResolution. There is also a VerticalResolution. But these tend to be the same. (Though you can add a line of code to check, if you want.)


Image Pixel Depth

The code to get the pixel information is slightly more complicated. Here's the line to add in C#:

string imagePixelDepth = Image.GetPixelFormatSize(img.PixelFormat).ToString();

And here it in VB Net:

Dim imagePixelDepth As String = Image.GetPixelFormatSize(img.PixelFormat).ToString()

After the Image Class, type a dot and then select GetPixelFormatSize. In between the round brackets of GetPixelFormatSize, you type img.PixelFormat. This will get you a value of something like 8, 16, 24, depending on the image. This size is in bits per pixel.


Display Image Information in the Text Box

Now we can transfer all that information to the text box. This is easy enough. Add these lines in C#:

txtImageInfo.Text += imageType + "\r\n";
txtImageInfo.Text += "Width = " + imageWidth + "\r\n";
txtImageInfo.Text += "Height = " + imageHeight + "\r\n";
txtImageInfo.Text += "Resolution = " + imageResolution + "\r\n";
txtImageInfo.Text += "Pixel Depth = " + imagePixelDepth + "\r\n";

Add these lines in VB Net:

txtImageInfo.Text &= imageType & vbNewLine
txtImageInfo.Text &= "Width = " & imageWidth & vbNewLine
txtImageInfo.Text &= "Height = " & imageHeight & vbNewLine
txtImageInfo.Text &= "Resolution = " & imageResolution & vbNewLine
txtImageInfo.Text &= "Pixel Depth = " & imagePixelDepth & vbNewLine

Your code should look like this in C#:

C# code that displays information about an image in a text box

And here's the VB Net version:

VB Net code that displays information about an image in a text box

Try it out. Click your button and locate an image on your computer. You should see something like this in the text box: (All the space at the bottom of the form is going to be used when we get the image's colors.)

A Windows Form showing and image in a picture box and information about the image


In the next lesson below, we'll get some information from an image taken with a camera, either the one on your phone or a digital camera. We'll only get this information if the image is a JPEG. This is the format that is used for the camera on your mobile/cell phone, and on a lot of digital cameras. Of course, not all JPEG images are taken with a camera. So we'll need to display an alternate message if no information is available. But the information we're going to get from the image is the date and time the photo was taken, the maker of the camera, and the camera model.

Image Metadata >>

Back to the C# NET Contents Page

Back to the VB NET Contents Page