I have an ASHX handler returning an image on page load. I need to add a class to the image dynamically depending on the dimensions of the image. I have tried doing this using the following methods:
Code Behind Method
cs
protected void Page_Load(object sender, EventArgs e)
{
int id = Convert.ToInt32(Request.QueryString["id"]);
image1.ImageUrl = "get_image.ashx?id=" + id;
}
public void classify_image_Load(object sender, EventArgs e)
{
if(image1.Width.Value > image1.Height.Value)
{
image1.CssClass = "landscape";
}
else
{
image1.CssClass = "portrait";
}
}
html
<asp:Image ID="image1" runat="server" OnLoad="classify_image_Load" />
This works on initial load, however on any postbacks (uploading a new image/rotating/cropping) it fails to apply the class correctly.
jQuery Method
js
$(window).load(function(){
$('#image1').load(function() {
if($(this).width() > $(this).height())
{
$(this).attr('class', 'landscape');
} else {
$(this).attr('class', 'portrait');
}
});
});
This method doesn't work at all, the image has no class assigned to it. I'm not sure if this is a timing issue with the ashx control or what.
ASHX Code
public class get_image : IHttpHandler
{
string file_path = ConfigurationManager.AppSettings["file_path"].ToString();
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
Image img;
if (!String.IsNullOrEmpty(context.Request.QueryString["id"]))
{
int id;
if (context.Request.QueryString["id"].IndexOf('?') > 0)
{
id = Int32.Parse(context.Request.QueryString["id"].Split('?')[0]);
}
else
{
id = Int32.Parse(context.Request.QueryString["id"]);
}
dbclassDataContext db = new dbclassDataContext();
photo d = (from p in db.photos
where p.id == id
select p).SingleOrDefault();
if (d != null)
{
if (!String.IsNullOrEmpty(d.filename))
{
img = Image.FromFile(file_path + "\\" + d.filename);
context.Response.ContentType = "image/" + d.filetype;
img.Save(context.Response.OutputStream, get_format(d.filetype));
}
else
{
img = Image.FromFile(file_path + "\\no_image.jpg");
context.Response.ContentType = "image/jpeg";
img.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
}
else
{
img = Image.FromFile(file_path + "\\no_image.jpg");
context.Response.ContentType = "image/jpeg";
img.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
}
else
{
img = Image.FromFile(file_path + "\\no_image.jpg");
context.Response.ContentType = "image/jpeg";
img.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
img.Dispose();
}
public bool IsReusable
{
get
{
return false;
}
}
private ImageFormat get_format(string ftype)
{
switch (ftype)
{
case "jpeg":
return ImageFormat.Jpeg;
case "png":
return ImageFormat.Png;
case "gif":
return ImageFormat.Gif;
default:
return ImageFormat.Jpeg;
}
}
}
I'm using Linq to pull the location and type from the database based on the users ID, I then return the image to the requesting page. This seems to be working fine but I included it in case there may be any issues I overlooked.
Question
I need to dynamically classify the Image based on its dimensions, I can do this either in .NET code behind or with jQuery. What am I doing wrong with the above methods that is causing it to not work correctly?
The .load event can have problems if the image is loaded from browser cache (see http://api.jquery.com/load-event/ ) - maybe you could try either disabling the caching or appending a time value to the image so the browser doesn't cache it, in case it's that issue that's hitting?