<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>.Net Bits and Pieces &#187; Web</title>
	<atom:link href="http://www.dotnetbitsandpieces.com/?cat=8&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetbitsandpieces.com</link>
	<description>What ever I find interesting to tell about .Net</description>
	<lastBuildDate>Sat, 06 Apr 2013 21:31:17 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>Caching bitmaps in the HttpContext.Current.Cache</title>
		<link>http://www.dotnetbitsandpieces.com/?p=29</link>
		<comments>http://www.dotnetbitsandpieces.com/?p=29#comments</comments>
		<pubDate>Tue, 22 Jan 2013 18:31:18 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Bitmap]]></category>

		<guid isPermaLink="false">http://www.dotnetbitsandpieces.com/?p=29</guid>
		<description><![CDATA[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) { &#38;nbsp;&#38;nbsp;HttpContext.Current.Cache.Insert(key, bmp, null, &#38;nbsp;&#38;nbsp;&#38;nbsp;&#38;nbsp;System.Web.Caching.Cache.NoAbsoluteExpiration, &#38;nbsp;&#38;nbsp;&#38;nbsp;&#38;nbsp;System.Web.Caching.Cache.NoSlidingExpiration, &#38;nbsp;&#38;nbsp;&#38;nbsp;&#38;nbsp;System.Web.Caching.CacheItemPriority.Normal, null); } &#160; public [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><span style="font-size: small">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.</br> So I wrote this small piece of code, just for this:</span>
<pre>

<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">public static void Cache(Bitmap bmp, String key)<br />
{<br />
&amp;nbsp;&amp;nbsp;HttpContext.Current.Cache.Insert(key, bmp, null,<br />
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.Web.Caching.Cache.NoAbsoluteExpiration,<br />
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.Web.Caching.Cache.NoSlidingExpiration,<br />
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.Web.Caching.CacheItemPriority.Normal, null);<br />
}<br />
&nbsp;<br />
public static Bitmap Fetch(String key)<br />
{<br />
&amp;nbsp;&amp;nbsp;return HttpContext.Current.Cache.Get(key) as BitmapCache;<br />
}</div></div>

</pre>
<p><span style="font-size: small">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 <strong>ArgumentException: Parameter is not valid</strong>.</span></p>
<p><span style="font-size: small">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. </span></p>
<p><span style="font-size: small">Now the code for caching the bitmap looks like</span></p>
<pre>

<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">public static void Cache(Bitmap bmp, String key)<br />
{<br />
&amp;nbsp;&amp;nbsp;//Store the image as a byte array. The bitmap its self maybe disposed<br />
&amp;nbsp;&amp;nbsp;//and this will retrieve a disposed image from the chache.<br />
&amp;nbsp;&amp;nbsp;using (var ms = new MemoryStream())<br />
<br />
&amp;nbsp;&amp;nbsp;bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);<br />
&amp;nbsp;&amp;nbsp;HttpContext.Current.Cache.Insert(key, new BitmapCache(ms.ToArray()), null,<br />
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.Web.Caching.Cache.NoAbsoluteExpiration,<br />
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.Web.Caching.Cache.NoSlidingExpiration,<br />
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.Web.Caching.CacheItemPriority.Normal, null);<br />
&amp;nbsp;&amp;nbsp;}<br />
<br />
public static Bitmap Fetch(String key)<br />
{<br />
&amp;nbsp;&amp;nbsp;byte[] bitmapAsArray= HttpContext.Current.Cache.Get(key) as BitmapCache;<br />
&amp;nbsp;&amp;nbsp;Bitmap bmp = null;<br />
&amp;nbsp;&amp;nbsp;try<br />
&amp;nbsp;&amp;nbsp;{<br />
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;using (var ms = new MemoryStream((byte[])bc.bitmapAsArray))<br />
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{<br />
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;bmp = Bitmap.FromStream(ms) as Bitmap;<br />
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}<br />
&amp;nbsp;&amp;nbsp;catch(Exception)<br />
&amp;nbsp;&amp;nbsp;{<br />
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return null;<br />
&amp;nbsp;&amp;nbsp;}<br />
&amp;nbsp;&amp;nbsp;return bmp;<br />
}</div></div>

</pre>
<p><span style="font-size: small">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.</style>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetbitsandpieces.com/?feed=rss2&#038;p=29</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
