Extracting Bitmap object from Cache object RRS feed

  • Question

  • User2064082449 posted
    Hi folks, I'm creating a set of images dynamically (for a primary navigation), that part is all sweet, however I want to cache the Bitmap objects rather than recreate them each time a page loads. I have the following code: Cache cache = System.Web.HttpContext.Current.Cache; if ( cache[HashImage(_text, _active)] == null ) { <snip> // Set the width and height // Width is calculated by the text length Bitmap image = new Bitmap(100, 20, PixelFormat.Format24bppRgb); // Set graphic object graphic = Graphics.FromImage(image); <snip> Response.ContentType="image/gif"; cache.Insert( "Image1", (Bitmap)image, null, DateTime.MaxValue, TimeSpan.FromSeconds(30)); image.Save(Response.OutputStream, ImageFormat.Gif); image.Dispose(); } else { Response.ContentType = "image/gif"; Bitmap image = new Bitmap(cache["Image1"]); image.Save(Response.OutputStream, ImageFormat.Gif); image.Dispose(); } However I get this error: Invalid parameter used. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentException: Invalid parameter used. Any ideas?
    Friday, July 2, 2004 12:55 AM

All replies

  • User-599719271 posted
    and that happens on this line, I expect : cache.Insert( "Image1", (Bitmap)image, null, DateTime.MaxValue, TimeSpan.FromSeconds(30)); ? the second parameter of this call should be type Object, if that clarifies things at all.
    Friday, July 2, 2004 2:04 AM
  • User2064082449 posted
    It happens on this line: image.Save(Response.OutputStream, ImageFormat.Gif);
    Friday, July 2, 2004 2:58 AM
  • User-599719271 posted
    OK, but there appear to be two of those. does it happen on one or the other or both? (or did I just misread the code? I'll go back in a sec)
    Friday, July 2, 2004 3:14 AM
  • User2064082449 posted
    It only happens when I refer to the cached object - when it initially creates the image, there's no problems at all.
    Friday, July 2, 2004 3:25 AM
  • User-599719271 posted
    OK, what if you swap the two method calls? the save to Response and the save to Cache? and what if you write from the Cached copy instead of from the image object?
    Friday, July 2, 2004 3:48 AM
  • User2064082449 posted
    I'm doing a bit of debugging, after it's added to the Cache, the object is recognised as type System.Drawing.Bitmap - however when I try to get it's Width or Height I get the same error as above...when I write out it's PixelFormat property it says "DontCare"
    Friday, July 2, 2004 4:35 AM
  • User-1372641848 posted
    Response.ContentType = "image/gif"; Bitmap image = new Bitmap(cache["Image1"]); <-- why this? image.Save(Response.OutputStream, ImageFormat.Gif); image.Dispose(); You already have bitmap object why recreate? You can write like Bitmap image = (Bitmap)cache["Image1"]; PS: I usually do same kind of thing but in slightly different way, I would convert bitmap(bitmap -> save -> to memorystream -> byte array) to byte array and then put byte array in cache, in this process i do not have to do extra processing with bitmap every time when image is served from cache.
    Friday, July 2, 2004 10:04 AM
  • User2064082449 posted
    Thanks mate, that worked a treat! MemoryStream mem = new MemoryStream(); image.Save(mem, ImageFormat.Gif); Cache.Insert( (string)HashImage(_text, _active), (byte[])mem.ToArray(), null, DateTime.MaxValue, TimeSpan.FromMinutes(30)); Response.ContentType="image/gif"; image.Save(Response.OutputStream, ImageFormat.Gif); image.Dispose(); } else { Response.ContentType = "image/gif"; MemoryStream mem = new MemoryStream((byte[])Cache[HashImage(_text, _active)]); Bitmap image = new Bitmap(mem); image.Save(Response.OutputStream, ImageFormat.Gif); image.Dispose();
    Friday, July 2, 2004 11:15 PM
  • User-1372641848 posted
    You can optimize you code by sending bytes directly to ResponseStream (avoids bitmap creation) Response.ContentType = "image/gif"; byte[] buffer = (byte[])Cache[HashImage(_text, _active)]; Response.BinaryWrite(buffer);
    Tuesday, July 6, 2004 1:30 PM