The browser would render a webpage faster if it knows the images dimensions. But how can I assign the Width and Height in ASP.NET automatically?
I can do a server control extending Image that would query the image size using GDI+ and assign those values to its own Width and Height properties.
But there are several things that worries me and would like to hear opinions:
- Cache: What is the best way to implement cache? I would like to cache the Height and Width values across all pages for each image.
- Which method should I use to query the image size? The constructor or another method (OnInit, OnPreRender, etc, etc)?
- Is it GDI+ the best option to query image size, or is there any other efficient way?
- How can I query the size for remotes images?
Of course I have an idea of how to do all that I mentioned, but I wanted to hear some thoughts.
http://code.google.com/speed/page-speed/docs/rendering.html#SpecifyImageDimensions
EDIT: This is what I have so far:
[ToolboxData("<{0}:AutoSizeImage runat=server></{0}:AutoSizeImage>")]
public class AutoSizeImage : Image
{
protected override void OnPreRender(EventArgs e)
{
if (Width.Value + Height.Value == 0)
{
if (IsLocal(ImageUrl))
{
using (System.Drawing.Image img = System.Drawing.Image.FromFile(Context.Server.MapPath(ImageUrl)))
{
Width = img.Width;
Height = img.Height;
}
}
else
{
System.Net.WebRequest request = System.Net.WebRequest.Create(ImageUrl);
using (System.Net.WebResponse response = request.GetResponse())
using (System.IO.Stream responseStream = response.GetResponseStream())
using (System.Drawing.Image img = System.Drawing.Image.FromStream(responseStream))
{
Width = img.Width;
Height = img.Height;
}
}
}
base.OnPreRender(e);
}
private bool IsLocal(string p)
{
return !(p.StartsWith("http://") || p.StartsWith("https://") || p.StartsWith("ftp://"));
}
}
It works beautifully, but I need to implement cache now.
I want to cache the output of this control for each value of ImageUrl, that is, I want the OnPreRender method to be called once for each image. Is that possible?
I am going to use Image Optimization Framework that generates sprites and writes the image width and height so no need to reinvent the wheel.
https://web.archive.org/web/20211020150102/https://www.4guysfromrolla.com/articles/101310-1.aspx