Handlers for Image RRS feed

  • Question

  • User-534649585 posted

    I have a web form with a table that i populate with images.

    Following is the code for retrieving the images from the database.

    I have this problem

    The page loads for example with 6 items and then each items fires

    a handler for the image but everytimes i have less images than i expect.

    And the images are in the database

    I am debugging locally with the ASP webserver

    My question is

    Is possible that the DataBase server cannot handle many requests?

    Or the Webserver cannot handle the requests?

    If i try to debug the handler the code is reentrant and seems many

    requests are concurrent.

    This happens only on some machine and never happened on the production one.

    Could you help me?




    <%@ WebHandler Language="C#" class="ShowThumbnails" %>

    using System; using System.Web; using System.IO;

    using System.Data; using MySql.Data.MySqlClient;

    using System.Drawing.Imaging; 

    public class ShowThumbnails : IHttpHandler {

        public void ProcessRequest(HttpContext context)     {

            Int32 picid;

            Int32 width;

            Int32 height;

             if (context.Request.QueryString["id"] != null)        

             {             picid = Convert.ToInt32(context.Request.QueryString["id"]);  


                          height = Convert.ToInt32(context.Request.QueryString["height"]);

                 }         else


            context.Response.ContentType = "image/jpeg";

            Stream strm = ShowThumbnail(picid);

            System.Drawing.Image image = System.Drawing.Image.FromStream(strm);

            System.Drawing.Image thumbnailImage = image.GetThumbnailImage(width, height,new System.Drawing.Image.GetThumbnailImageAbort(target), IntPtr.Zero);

            Stream imageStream = new MemoryStream();

            thumbnailImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);

            byte[] buffer = new byte[4096];

            imageStream.Position = 0;

            int byteSeq = ((Stream)imageStream).Read(buffer, 0, 4096);

            while (byteSeq > 0)

            {    context.Response.OutputStream.Write(buffer, 0, byteSeq);

                 byteSeq = imageStream.Read(buffer, 0, 4096);         }


        private bool target()     {

            return false;     }

        public Stream ShowThumbnail(int picid)    

     {         DataDB db = new DataDB();


            MySqlConnection connection = db.Connection;

            string sql = "SELECT image FROM items WHERE id =" + picid.ToString();//  @ID";

            MySqlCommand cmd = new MySqlCommand(sql, connection);

            cmd.CommandType = CommandType.Text;

            object img = cmd.ExecuteScalar();

            try         {

                 return new MemoryStream((byte[])img);


             catch         {

                 return null;


             finally         {




        public bool IsReusable {         get {             return false;         }     }



    Monday, February 18, 2013 1:33 AM

All replies

  • User-760709272 posted

    Does it work when you're not debugging it?

    Monday, February 18, 2013 4:28 AM
  • User-534649585 posted

    Debugging on a different phisical machine it works.

    When publish on a production site it works.

    Debugging on Windows 7 home edition it does not work

    Debugging on virtual machine XP hosted on windows 7 it does not work

    I moved the site from an old machine where i developed the site on a

    new machine and start to see this problem....





    Monday, February 18, 2013 5:02 AM
  • User-760709272 posted

    I wouldn't worry about it.  When debugging the same code in multiple threads it can get awkward as your code "jumps around" as the different threads are in different sections, and you have to remember that you're tying up all the threads so what is probably happening is that some requests are just timing out which is why some of your images are missing when you debug, but not when you run naturally.  If you want to test your handler then use it on a page that only has one image so you can step through it better.

    Monday, February 18, 2013 5:27 AM
  • User-534649585 posted

    Please, is there a way not to timeout the handler?

    What is supposed to time out, the database connection or the webserver?

    If image is empty, means that ExecuteScalar() returns null. So

    is it possible the problem is coming from the Database?

    I could simply change the timeout of the connection to easily debug.





    Monday, February 18, 2013 7:39 AM
  • User-760709272 posted

    It could be the browser saying that it simply isn't going to wait any longer.  Modern browsers have good development tools if you press F12 and you can see the network activity which might help decide if it is timing out.  Or you can use something like Fiddler2 which is independent of your browser.

    If you view a page with 6 images, and you have a breakpoint in your handler code then the browser will execute 6 requests (depending on the browser and its settings, it might request less) and each of those requests is blocked until you have stepped through them.  So the first few requests might get done in time, but by the time you have stepped through 4 or 5 processes, the 6th process might have taken so long to execute that either the browser gave up, or IIS terminated the thread to protect its own resources.

    As long as it doesn't happen when you're not debugging, I don't see that it's a problem.

    Monday, February 18, 2013 7:51 AM
  • User-534649585 posted

    Probably you are correct, it is something related to timeout.

    But now i have some other problems, not only images are empty

    also the query on the records return empty.

    Need to close Visual Studio and reopen.

    Then i see the records. I think it is something connected to the database...



    Tuesday, February 19, 2013 12:43 AM
  • User-534649585 posted

    With MySql 5.1.44 works well.



    Tuesday, February 19, 2013 7:50 AM