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 )
{
// 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);
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?
biomechanic2...
Member
110 Points
27 Posts
Extracting Bitmap object from Cache object
Jul 02, 2004 04:55 AM|LINK
Cache cache = System.Web.HttpContext.Current.Cache; if ( cache[HashImage(_text, _active)] == null ) { // 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); 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?