Monthly Archives: January 2013

Caching bitmaps in the HttpContext.Current.Cache

I was working on a website that is generating images on the fly. Generating these images takes some time and therefore they needed to be cached.
So I wrote this small piece of code, just for this:


public static void Cache(Bitmap bmp, String key)
{
  HttpContext.Current.Cache.Insert(key, bmp, null,
    System.Web.Caching.Cache.NoAbsoluteExpiration,
    System.Web.Caching.Cache.NoSlidingExpiration,
    System.Web.Caching.CacheItemPriority.Normal, null);
}
 
public static Bitmap Fetch(String key)
{
  return HttpContext.Current.Cache.Get(key) as BitmapCache;
}

That was easy and straight forward, however this was one of these cases where things are not what they seem to be. Generating the image went without any issue and showed on the webpage. However, getting the image from the cache did not return an image but an error message in stead. The error message was ArgumentException: Parameter is not valid.

When trying to view the properties of the bitmap in debug mode, all of the properties showed the same error in stead of their actual value. After scratching my head a few times I found why, the bitmap was disposed. The above code puts a reference to the bitmap in the cache, so when the bitmap gets disposed it is not longer accessible from the cache. I first thought of creating a copy of the bitmap and put that in the cache, but then I found a much nicer solution on the web: Store the bitmap as a byte array.

Now the code for caching the bitmap looks like


public static void Cache(Bitmap bmp, String key)
{
  //Store the image as a byte array. The bitmap its self maybe disposed
  //and this will retrieve a disposed image from the chache.
  using (var ms = new MemoryStream())

  bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
  HttpContext.Current.Cache.Insert(key, new BitmapCache(ms.ToArray()), null,
      System.Web.Caching.Cache.NoAbsoluteExpiration,
      System.Web.Caching.Cache.NoSlidingExpiration,
      System.Web.Caching.CacheItemPriority.Normal, null);
  }

public static Bitmap Fetch(String key)
{
  byte[] bitmapAsArray= HttpContext.Current.Cache.Get(key) as BitmapCache;
  Bitmap bmp = null;
  try
  {
    using (var ms = new MemoryStream((byte[])bc.bitmapAsArray))
    {
      bmp = Bitmap.FromStream(ms) as Bitmap;
    }
  catch(Exception)
  {
    return null;
  }
  return bmp;
}

and this works everytime. There is a slight overhead in transforming the byte[] to a bitmap, but this is much less then generating the bitmap everytime. When you want to output the image direct you could even stream the byte[] directly and thus avoiding the overhead of transforming it to the bitmap.

Enum.HasFlag() versus Bitwise comparison

Call me a bit slow, but I recently discovered that Microsoft has implemented an HasFlag method on the Enum class since .Net 4. The method is meant as replacement for the bitwise comparison between values of an enumeration that has the FlagAttribute. For more information about the the FlagAttribute and bitwise comparison you can have a look at this blog.

For a project I am working on I am using the bitwise comparison extensively, so using the HasFlag method would make the code much more readable. Before using it I wanted to get some more information about how the HasFlag method works and I found this article. It shows the inner workings of the method, but also mentions that there is a performance cost to pay. However it does not mention what the cost is.

I therefor did my own test to see what the performance overhead would be and if it was worth to change. Continue reading

Just another blog

This is the first blog post on my new blog DotNet Bits and Pieces. Like the name says I will blog here about the things that I will run into while working with the .Net Framework.

Because of my work most post will probably be about Visual Studio and C#, but we will see when time goes by.