How to get the height and width of dynamically generated Images and adjust it

In this article I am going to explain how we can get the height and width of images which is dynamically displayed from the database and resizing it acoording to its initial height and width. There will not be any front page image tag in this example.

The image height and width adjust is an easy task since there is height and width property for image tag in ASP.NET. But if we have to adjust the height and width according to its original resolution, first we have to get the original resolution of the image. In this example first we will get the height and width and after that we will resize it according to the initial values.
In this example I am using placeholder at the front end to display the image. My aspx page looks like this

<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

At code behind try something like this

protected void Page_Load(object sender, EventArgs e)
Image postimg = new Image();
SqlConnection cn = new SqlConnection(“your connection string”);
SqlCommand cm = new SqlCommand("select imagepath from imagetable",cn);
postimg.ImageUrl = (string)cm.ExecuteScalar();
string PicturePath = Server.MapPath(""+postimg.ImageUrl+"");
System.Drawing.Image image1 = System.Drawing.Image.FromFile(PicturePath);
int width = image1.Width;
int height = image1.Height;

if (height >= 250 || width >= 400)
postimg.Height = 250;
postimg.Width = 360;

By this way you can get the height and width of any controls like gridview,datalist,textbox etc. that has these height and width property.
This height and width adjusting method can be used to make your webpage more responsive to the client screen.


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: