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 image size using binary reader in VB.net – C#.net.

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

In this simple article you will learn that how to get image size using binary reader in VB.net – C#.net.

 File Stream:

First create an object of file stream class. Constructor of filestream class takes three parameters. First is the file path, second file mode and file access mode.

Binary Reader:

After creating the filestream class object, create an object of binary reader class. Constructor of binary reader class takes object of stream class.

Image size:

After doing the above steps, you will to expose the access to that stream and also have to define the current position in that stream for this purpose we have a property of binary reader class named basestream.position. This property takes and returns the long value.

Now to read the size we have a function of binary reader class named ReadInt32(). This function returns value of integer type that is the size of image in bytes.

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

Now write the following code on Button Click Event:

C#

private void btn_size_Click(object sender, EventArgs e)

        {

            try

            {

                br = new BinaryReader(file);

                br.BaseStream.Position = 0x12;

                int s = br.ReadInt32();

                listBox1.Items.Add(s);

                s = br.ReadInt32();

                listBox1.Items.Add(s);

                s = br.ReadInt16();

                listBox1.Items.Add(s);

                s = br.ReadInt16();

                listBox1.Items.Add(s);

                s = br.ReadInt16();

                listBox1.Items.Add(s);

                file.Close();

                br.Close();

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

VB

Private Sub btn_size_Click(ByVal sender As ObjectByVal As EventArgs)
        
Try 
            
br = New BinaryReader(file)
            br.BaseStream.Position 
18
            
Dim As Integer = br.ReadInt32
            listBox1.Items.Add(s)
            s 
br.ReadInt32
            listBox1.Items.Add(s)
            s 
br.ReadInt16
            listBox1.Items.Add(s)
            s 
br.ReadInt16
            listBox1.Items.Add(s)
            s 
br.ReadInt16
            listBox1.Items.Add(s)
            file.Close
            br.Close
        
Catch ex As Exception
            
MessageBox.Show(ex.Message)
        
End Try
    End Sub

This is simple code to read size of image using binary reader.

Now write the following code on FORM LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

        {          

            this.Text = "Devasp  Application";

            file = new FileStream("Penguins.bmp", FileMode.Open, FileAccess.Read);

        }

VB

Private Sub Form1_Load(ByVal sender As ObjectByVal As EventArgs)
        
Me.Text "Devasp  Application"
        
file = New FileStream("Penguins.bmp", FileMode.Open, FileAccess.Read)
    
End Sub

This simple article tells that how to get image size using binary reader in VB.net – C#.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Draw shapes to bitmap image in memory using Graphics class in VB.net – C#.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net