locked
Basic question about image handler RRS feed

  • Question

  • User851230320 posted

    I was not allowed to write files on the server; so I tried using a general image handler; Is it advised to use cached memoryStream of the image? I tried it but sometimes it doesn't seem to work, though works reasonably well other times! Is there a problem with caching? Is there any other way around? Thanks for your help.

    public void ProcessRequest(HttpContext ctx)
    {
    string id = ctx.Request.QueryString["id"];
    string type = ctx.Request.QueryString["type"];
    MemoryStream ms = new MemoryStream();
    ms = (
    MemoryStream)ctx.Cache[id];
    byte[] imageBinary = ms.GetBuffer();
    int len = imageBinary.GetUpperBound(0);
    ctx.Response.ContentType =
    "image/" + type;
    ctx.Response.OutputStream.Write(imageBinary, 0, len);
    ms.Close();
    }

    Monday, January 8, 2007 7:51 AM

Answers

  • User300685930 posted

    Where are you actually loading Cache[id]?  The way I ususally handle this is to check if cache is available, then if not, I get the image frmo the source.  I'll paste some code I've written below. 

     HTH's.

    string cacheKey = "DisplayjAd_" + strParam1;

    byte[] byteArray = null;

    if (HttpContext.Current.Cache[cacheKey] == null)

    {    // get the data

         // refersh the cache with Cache.Insert(..

        

    }

    else

    {   /// display the image from cache

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 8, 2007 10:51 AM
  • User300685930 posted

    If you want the cache not to disappear, you need to set the option like this:

    HttpContext.Current.Cache.Insert(cacheKey, cp,null,cacheExpiration, Cache.NoSlidingExpiration,CacheItemPriority.NotRemovable,null);

    Keep in mind though it will have implications on your server memory.  you should make sure you make it time out at some point

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 8, 2007 2:16 PM
  • User300685930 posted

    I think the reason the cache doesn't last is that most hosting environments restrict the amount of memory available to each web site to a small amount (like 128 or 256mb).  this puts a lot of pressure on the cache.  I wrote a CAPTCHA control that I blogged on and while it was under heavy usage (a codecamp), the cache was not even lasting seconds.  That is how I found about about the feature.  I think it is a good design though. Just have to understand the implications (like we both do now).

     Good luck.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 8, 2007 10:27 PM

All replies

  • User300685930 posted

    Where are you actually loading Cache[id]?  The way I ususally handle this is to check if cache is available, then if not, I get the image frmo the source.  I'll paste some code I've written below. 

     HTH's.

    string cacheKey = "DisplayjAd_" + strParam1;

    byte[] byteArray = null;

    if (HttpContext.Current.Cache[cacheKey] == null)

    {    // get the data

         // refersh the cache with Cache.Insert(..

        

    }

    else

    {   /// display the image from cache

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 8, 2007 10:51 AM
  • User1178851682 posted

    There is nothing wrong with cache

    well, if you have problem with cache....then you can try use Session

    Monday, January 8, 2007 10:53 AM
  • User851230320 posted

    Thanks for the help. I've written a user control which creates graphs and uses the handler to project images; I also tried checking whether cache was empty in the handler; but cache turned out to be empty in some tries; again fully working other times! And sometimes I get the image after I refresh the page for a few times. Unfortunately, the user control I have written uses many variales and it is not very easy to address it if cache is found empty in the handler. Am I missing something even more basic?

    MemoryStream ms = new MemoryStream();
    curBitmap.Save(ms,
    ImageFormat.Bmp);
    Cache.Insert(cache_name, ms);
    System.Web.UI.WebControls.
    Image newImage = new System.Web.UI.WebControls.Image();
    newImage.ImageUrl =
    "~/ImageHandler.ashx?id=" + cache_name + "&type=bmp";
    graphPlaceHolder.Controls.Add(newImage);

    Monday, January 8, 2007 11:23 AM
  • User300685930 posted

    If you want the cache not to disappear, you need to set the option like this:

    HttpContext.Current.Cache.Insert(cacheKey, cp,null,cacheExpiration, Cache.NoSlidingExpiration,CacheItemPriority.NotRemovable,null);

    Keep in mind though it will have implications on your server memory.  you should make sure you make it time out at some point

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 8, 2007 2:16 PM
  • User851230320 posted

    Thanks for the great help; the solution worked very well;but I was wondering why these caches expired so quickly; meanwhile, I was going to ask if there was any better way to export data to the image handler.

    Monday, January 8, 2007 7:14 PM
  • User300685930 posted

    I think the reason the cache doesn't last is that most hosting environments restrict the amount of memory available to each web site to a small amount (like 128 or 256mb).  this puts a lot of pressure on the cache.  I wrote a CAPTCHA control that I blogged on and while it was under heavy usage (a codecamp), the cache was not even lasting seconds.  That is how I found about about the feature.  I think it is a good design though. Just have to understand the implications (like we both do now).

     Good luck.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 8, 2007 10:27 PM