Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3670108
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:19:27+00:00 2026-05-19T02:19:27+00:00

I have an ASHX handler returning an image on page load. I need to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T02:19:28+00:00Added an answer on May 19, 2026 at 2:19 am

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an ashx handler to render image thumbnails from images in a
I have an image upload handler (.ASHX). I am storing image meta data in
I have created an ashx handler that returns an image to my flex app.
I have a handler (.ashx) file that I'm using to deal with Requests. Depending
I have a function: window.onbeforeunload = function () { $.get(Handler.ashx, mode=saveuser&user= + $(#username).text(), function
I have ASHX HTTPHandler which implements IRequiresSessionState. When I call this handler, and call
I have an *.ashx (handler) with this code: public void ProcessRequest (HttpContext context) {
I have an ashx handler which takes the 'html' property from the request, then
I have an ASHX handler that I am using to display images that are
I have written a small ASHX handler that uses a small state object that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.