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?
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.
RTFM - straight talk for web developers. Unmoderated, uncensored, occasionally unreadable
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?
RTFM - straight talk for web developers. Unmoderated, uncensored, occasionally unreadable
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"
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.
Jigar Desai -----------------------
Do not forget to "Mark as Answer" on the post that helped you.
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?Atrax
All-Star
18705 Points
3733 Posts
Re: Extracting Bitmap object from Cache object
Jul 02, 2004 06:04 AM|LINK
Jason Brown - MVP, IIS
biomechanic2...
Member
110 Points
27 Posts
Re: Extracting Bitmap object from Cache object
Jul 02, 2004 06:58 AM|LINK
Atrax
All-Star
18705 Points
3733 Posts
Re: Extracting Bitmap object from Cache object
Jul 02, 2004 07:14 AM|LINK
Jason Brown - MVP, IIS
biomechanic2...
Member
110 Points
27 Posts
Re: Extracting Bitmap object from Cache object
Jul 02, 2004 07:25 AM|LINK
Atrax
All-Star
18705 Points
3733 Posts
Re: Extracting Bitmap object from Cache object
Jul 02, 2004 07:48 AM|LINK
Jason Brown - MVP, IIS
biomechanic2...
Member
110 Points
27 Posts
Re: Extracting Bitmap object from Cache object
Jul 02, 2004 08:35 AM|LINK
Jigar
Contributor
4629 Points
935 Posts
Re: Extracting Bitmap object from Cache object
Jul 02, 2004 02:04 PM|LINK
-----------------------
Do not forget to "Mark as Answer" on the post that helped you.
biomechanic2...
Member
110 Points
27 Posts
Re: Extracting Bitmap object from Cache object
Jul 03, 2004 03:15 AM|LINK
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();Jigar
Contributor
4629 Points
935 Posts
Re: Extracting Bitmap object from Cache object
Jul 06, 2004 05:30 PM|LINK
-----------------------
Do not forget to "Mark as Answer" on the post that helped you.