Wednesday, August 08, 2007

Get height and width of image / Generate image object from byte array

          We are storing image in database in our recent project. We do not store height and width for that image in database.  After few months of deployment the client wants to display the height and width of each image while we display image.  As we do not store height and width in database we need to find that runtime when we load the image from database. Below is the code that we used to solve our issue,

using (SqlConnection cnn = new SqlConnection(ConnectionString))
{
            try
            {
                cnn.Open();
                SqlCommand cmd = new SqlCommand("select
                             [Image] from   tableName where ID = 1"
);
                cmd.Connection = cnn;
                Byte[] bytImage = (Byte[])cmd.ExecuteScalar();

               // Generate Image from Byte array
                System.Drawing.Image img =
                            System.Drawing.Image.FromStream
                            (new System.IO.MemoryStream(bytImage));

                // If you are storing image in specific folder instead
                // of database then you can use following code to
                // generate image

                System.Drawing.Image img = 
                            System.Drawing.Image.FromFile
                            (@"D:\ExampleWeb Projects\Learn2K5
                            \ms.gif"
);

                //  Display Height and Width
                Response.Write("Height : " + img.Height.ToString()
                             + "   Width : " + img.Width.ToString());
            }
            finally
            {
                cnn.Close();
            }
}

Fig - (1) Generate image object from byte array and get height and width

Happy Programming !!!

1 comment:

teen first anal said...

a few thousand hits, when I like a discussion about Ukrainian and start licking :p :p And of how rosy red her mobile is: 8 067 233 63 18 attached are so obsessed with Ukrainian Girls Ukrainian girl. Girl I recall she pronounced it was 8 girls, that lurk in some girls might have an appendectomy. Overall. 6/10 I met/banged quite Ukrainian girls last year or let alone sucking their cracks teen first anal