Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Get height, width, frame count and horizontal - vertical resolution of image in VB.net - C#.net.

Author: Shehzad Hemani
Download Source Code : 1062_source.zip

In this simple article you will learn that how we can get height, width, frame count, vertical resolution and horizontal resolution of image in VB and C#.net.

 Get Height:

If you want to get the height of image in pixels for this purpose image class provides us a property named height. This property returns an integer value that value tells the height in pixels.

To demonstrate make a window application. Drag one button and a picture box on form.

Now write the following code on Button click event:

C#

private void btn_getHeight_Click(object sender, EventArgs e)

        {

            try

            {

                Bitmap img = new Bitmap(pictureBox1.Image);

                int height = 0;

                height = img.Height;

                MessageBox.Show("Height of pixels :" + height.ToString());

            }

            catch (Exception ex) { MessageBox.Show(ex.Message); }

        }

VB

Private Sub btn_getHeight_Click(ByVal sender As ObjectByVal As EventArgs)
        
Try 
            Dim 
img As Bitmap = New Bitmap(pictureBox1.Image)
            
Dim height As Integer = 0
            
height img.Height
            MessageBox.Show((
"Height of pixels :" + height.ToString))
        
Catch ex As Exception
            
MessageBox.Show(ex.Message)
        
End Try
    End Sub

This is simple code to get height of image.

Get Width:

If you want to get the width of image in pixels for this purpose image class provides us a property named width. This property returns an integer value that value tells the width in pixels.

To demonstrate make a window application. Drag one button and a picture box on form.

Now write the following code on Button click event:

C#

private void btn_getwidth_Click(object sender, EventArgs e)

        {

            try

            {

                Bitmap img = new Bitmap(pictureBox1.Image);

                int width = 0;

                width = img.Width;

                MessageBox.Show("width in pixels :" + width.ToString());

            }

            catch (Exception ex) { MessageBox.Show(ex.Message); }

        }

VB

Private Sub btn_getwidth_Click(ByVal sender As ObjectByVal As EventArgs)
        
Try 
            Dim 
img As Bitmap = New Bitmap(pictureBox1.Image)
            
Dim width As Integer = 0
            
width img.Width
            MessageBox.Show((
"width in pixels :" + width.ToString))
        
Catch ex As Exception
            
MessageBox.Show(ex.Message)
        
End Try
    End Sub

This is simple code to get width of image.

Get Horizontal Resolution:

If you want to get the horizontal resolution of image for this purpose image class provides us a property named HorizontalResolution. This property returns float value that value tells the resolution in pixels per inch.

To demonstrate make a window application. Drag one button and a picture box on form.

Now write the following code on Button click event:

C#

private void btn_gethorizontalresolution_Click(object sender, EventArgs e)

        {

            try

            {

                Bitmap img = new Bitmap(pictureBox1.Image);

                float res = 0;

                res = img.HorizontalResolution;

                MessageBox.Show("Resolution :" + res.ToString());

            }

            catch (Exception ex) { MessageBox.Show(ex.Message); }

        }

VB

Private Sub btn_gethorizontalresolution_Click(ByVal sender As ObjectByVal As EventArgs)
        
Try 
            Dim 
img As Bitmap = New Bitmap(pictureBox1.Image)
            
Dim res As Single = 0
            
res img.HorizontalResolution
            MessageBox.Show((
"Resolution :" + res.ToString))
        
Catch ex As Exception
            
MessageBox.Show(ex.Message)
        
End Try
    End Sub

This is simple code to get horizontal resolution of image.

Get Vertical Resolution:

If you want to get the Vertical resolution of image for this purpose image class provides us a property named verticalResolution. This property returns float value that value tells the resolution in pixels per inch.

To demonstrate make a window application. Drag one button and a picture box on form.

Now write the following code on Button click event:

C#

private void btn_vertical_Click(object sender, EventArgs e)

        {

            try

            {

                Bitmap img = new Bitmap(pictureBox1.Image);

                float res = 0;

                res = img.VerticalResolution;

                MessageBox.Show("Resolution :" + res.ToString());

            }

            catch (Exception ex) { MessageBox.Show(ex.Message); }

        }

VB

Private Sub btn_vertical_Click(ByVal sender As ObjectByVal As EventArgs)
        
Try 
            Dim 
img As Bitmap = New Bitmap(pictureBox1.Image)
            
Dim res As Single = 0
            
res img.VerticalResolution
            MessageBox.Show((
"Resolution :" + res.ToString))
        
Catch ex As Exception
            
MessageBox.Show(ex.Message)
        
End Try
    End Sub

This is simple code to get vertical resolution of image.

Get Frames Count:

If you want to get the number of frames in an image in pixels for this purpose image class provides us a function named GetFrameCount(). This function returns an integer value and takes object of framedimensions class..

To demonstrate make a window application. Drag one button and a picture box on form.

Now write the following code on Button click event:

C#

private void btn_getframecount_Click(object sender, EventArgs e)

        {

            try

            {

                Bitmap img = new Bitmap(pictureBox1.Image);

                int c = 0;

                System.Drawing.Imaging.FrameDimension fd=new System.Drawing.Imaging.FrameDimension(img.FrameDimensionsList[0]);

                c = img.GetFrameCount(fd);

                MessageBox.Show("count :" + c.ToString());

            }

            catch (Exception ex) { MessageBox.Show(ex.Message); }

        }

VB

Private Sub btn_getframecount_Click(ByVal sender As ObjectByVal As EventArgs)
        
Try 
            Dim 
img As Bitmap = New Bitmap(pictureBox1.Image)
            
Dim As Integer = 0
            
Dim fd As System.Drawing.Imaging.FrameDimension = New System.Drawing.Imaging.FrameDimension(img.FrameDimensionsList(0))
            c 
img.GetFrameCount(fd)
            MessageBox.Show((
"count :" + c.ToString))
        
Catch ex As Exception
            
MessageBox.Show(ex.Message)
        
End Try
    End Sub

This is simple code to get number of frames in an image.

Now write the following code on FORM LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

        {          

            this.Text = "DEVASP IMAGE APPLICATION";

        }

VB

 Private Sub Form1_Load(ByVal sender As ObjectByVal As EventArgs)
        
Me.Text "DEVASP IMAGE APPLICATION"
    
End Sub

This simple article tells that how we can get height, width, frame count, vertical resolution and horizontal resolution of image in VB and C#.net.

Article Comments
Hi Shehzad,
Can we find the Real Image height.Suppose i had taken Image of an object from Windows mobile,by using your code can i find the height of an object(like 5m or 60 cm).

Posted on 8/7/2010 7:25:35 AM by Syed Shakeer Hussain

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Get – set pixels and RGB Values of image in VB.net - C#.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net