0

I built the ASHX Image Handler which extracts the Images stored in the SQL Server. It works beautifully, if I show the image separately. But If I try to show them in the Grid and show many pictures simultaneously, some of the pictures are not showing randomly.

When I tried to refresh the page, some of the images disappear while some appears again. It is completely rendering the images randomly. Please see the image below for 4 screenshots.

enter image description here

The following are my codes for the Handler. I tried to change the IsReusable True & False, but no luck. Could you please advise me how I could solve this problem? Thanks.

public class Photo : IHttpHandler
{

    #region IHttpHandler Members

    public bool IsReusable
    {            
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        HttpRequest request = context.Request;

        if (!string.IsNullOrEmpty(request.QueryString["id"]) && !string.IsNullOrEmpty(request.QueryString["type"]))
        {
            //this hash table contain the SP parameter
            DAMSSQL db = DAMSSQL.GetInstance("DBName");

            SqlCommand cmd = new SqlCommand("dbo.GetImage");
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = int.Parse(request.QueryString["id"]);
            cmd.Parameters.Add("@Type", SqlDbType.Int).Value = int.Parse(request.QueryString["type"]);

            object obj = db.ExecuteScalar(cmd);

            if (obj != null)
            {
                byte[] imageArray = (byte[])obj;

                //checking byte[] 
                if (imageArray != null && imageArray.Length > 0)
                {   
                    context.Response.ContentType = "image/jpeg";
                    context.Response.BinaryWrite(imageArray);
                }
            }
            else
            {   
                context.Response.StatusCode = 404;
                context.Response.StatusDescription = "The requested image is not found in the system.";
                context.Response.End();

            }
        }
        else
        {
            context.Response.StatusCode = 500;
            context.Response.StatusDescription = "The incoming parameters are not correct.";
            context.Response.End();                
        }
    }

    #endregion
}
TTCG
  • 8,805
  • 31
  • 93
  • 141

1 Answers1

1

I asked a similar question here:

Streaming Databased Images Using HttpHandler

I ended up using a DataReader instead and it solved my problem with the images not all rendering.

Community
  • 1
  • 1
IrishChieftain
  • 15,108
  • 7
  • 50
  • 91