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
<body>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</body>
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);
cn.Open();
postimg.ImageUrl = (string)cm.ExecuteScalar();
cn.Close();
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;
}
PlaceHolder1.Controls.Add(postimg);
}
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.