<?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</title>
	<atom:link href="http://www.dotnetbitsandpieces.com/?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>Document Generation with OOXML Part 3</title>
		<link>http://www.dotnetbitsandpieces.com/?p=96</link>
		<comments>http://www.dotnetbitsandpieces.com/?p=96#comments</comments>
		<pubDate>Sat, 06 Apr 2013 21:31:17 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://www.dotnetbitsandpieces.com/?p=96</guid>
		<description><![CDATA[In a previous post I explained how the Document Generator library can be used with Word templates and how to use the styles defined in those templates. Besides using the styles in the template it should also be possible to insert specific content in the document. For example placing the title in the header of [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><span style="font-size: small">In a previous <a href="http://www.dotnetbitsandpieces.com/?p=83">post</a> I explained how the <a href="http://docgenerator.codeplex.com/">Document Generator library</a> can be used with Word templates and how to use the styles defined in those templates. Besides using the styles in the template it should also be possible to insert specific content in the document. For example placing the title in the header of a document or the author of the document. Before Word 2007 and OOXML you could use fields or create a WordML file with your own tags in it to insert content programmaticly. With OOXML you can use <a href="http://office.microsoft.com/en-us/help/content-controls-HA010030750.aspx">Content Controls</a> for this.</span><br />
<span style="font-size: small">The Document Generator uses content controls to insert a information at specified places in the documents. The content control types that are supported are:</p>
<ol>
<li>Text</li>
<li>Rich Text</li>
<li>Picture</li>
</ol>
<p>The content control must be defined in the OOXML document or template that is used with the library. This can be done from the Developer Tab in Word 2007 or higher.<br />
 </span></p>
<div class="codecolorer-container csharp geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-style: italic;">//Create text document from a template</span><br />
var document <span style="color: #008000;">=</span> TextDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;MyDocument.docx&quot;</span>, <span style="color: #666666;">&quot;MyTemplate.dotx&quot;</span>, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">//Insert the author of document, where Author is the name of the content </span><br />
<span style="color: #008080; font-style: italic;">//control and Shakespeare is the text that will be inserted.</span><br />
document<span style="color: #008000;">.</span><span style="color: #0000FF;">ReplaceCustomTag</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Author&quot;</span>, <span style="color: #666666;">&quot;W. Shakespeare&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">//Insert an image of the author, where AuthorImage is the name of the content </span><br />
<span style="color: #008080; font-style: italic;">//control and Shakespeare.png is the image.</span><br />
document<span style="color: #008000;">.</span><span style="color: #0000FF;">ReplaceCustomTag</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;AuthorImage&quot;</span>, <span style="color: #666666;">&quot;Shakespeare.png&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p><span style="font-size: small">The method name is the same for images and text. The library will determine the type of content control to find depending on the type that is given. When a string is given the library will look for a Picture content control and when a string is provided it will look for a Text control.<br />
When it cannot find a match on both name and type it will ignore the replacement.<br />
When a content control is defined more then once in the document it will insert the content for all the controls.</p>
<p>In this post I showed you how you can insert content such as text and images at specific places in documents by using Content Controls.<br />
In the next part I will show how you can create list and tables.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetbitsandpieces.com/?feed=rss2&#038;p=96</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Document Generation with OOXML Part 2</title>
		<link>http://www.dotnetbitsandpieces.com/?p=83</link>
		<comments>http://www.dotnetbitsandpieces.com/?p=83#comments</comments>
		<pubDate>Thu, 04 Apr 2013 20:47:49 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Document Generation]]></category>
		<category><![CDATA[OOMXL]]></category>

		<guid isPermaLink="false">http://www.dotnetbitsandpieces.com/?p=83</guid>
		<description><![CDATA[When creating a document from Word templates are used when creating documents. These can be default templates or templates created by users. It is their for essential that these templates can be used when generating OOXML documents. With the Document Generator library templates can be loaded when a new document is created. //Load a template [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><span style="font-size: small">When creating a document from Word templates are used when creating documents. These can be default templates or templates created by users. It is their for essential that these templates can be used when generating OOXML documents. With the Document Generator library templates can be loaded when a new document is created.</span></p>
<div class="codecolorer-container csharp geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-style: italic;">//Load a template from file. document is the name of the new document.</span><br />
<span style="color: #008080; font-style: italic;">//template is the name and full path of the template file</span><br />
<span style="color: #008080; font-style: italic;">//true indicates that the document can override documents with the same name.</span><br />
var document <span style="color: #008000;">=</span> TextDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span>document, template, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">//Load a template from a stream</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var stream <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> FileStream<span style="color: #008000;">&#40;</span>templateFile, FileMode<span style="color: #008000;">.</span><span style="color: #0000FF;">Open</span>, FileAccess<span style="color: #008000;">.</span><span style="color: #0000FF;">Read</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var reader <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StreamReader<span style="color: #008000;">&#40;</span>stream<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//stream is the stream of the template.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var document <span style="color: #008000;">=</span> TextDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span>stream<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p><span style="font-size: small">Once the template is loaded the styles defined in the template can be used. As long as the styles are the default for the header and the paragraphs only the level needs to be defined.</span></p>
<div class="codecolorer-container csharp geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-style: italic;">//This adds a new paragraph with the paragraph text and heading text.</span><br />
<span style="color: #008080; font-style: italic;">//The styles used are Heading2 and Normal2. When the styles cannot be found then</span><br />
<span style="color: #008080; font-style: italic;">//Heading1 and normal will be used as styles.</span><br />
var paragraph <span style="color: #008000;">=</span> document<span style="color: #008000;">.</span><span style="color: #0000FF;">AddParagraph</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Paragraph Text&quot;</span>, <span style="color: #666666;">&quot;Heading Text&quot;</span>, <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p><span style="font-size: small">Templates can also define custom styles. When the name of the style in the template is known the style can be used with the Document Generator library.<br />
The style can be applied to paragraphs and headers</p>
<div class="codecolorer-container csharp geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-style: italic;">//Creates a paragraph and assigns it the MyParagraphStyle.</span><br />
var paragraph <span style="color: #008000;">=</span> document<span style="color: #008000;">.</span><span style="color: #0000FF;">AddParagraph</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;This is the paragraph&quot;</span>, <span style="color: #666666;">&quot;MyParagraphStyle&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">//Creates a paragraph and assigns it the MyParagraphStyle.</span><br />
<span style="color: #008080; font-style: italic;">//and gives it a header with the style MyHeaderStyle</span><br />
var paragraph <span style="color: #008000;">=</span> document<span style="color: #008000;">.</span><span style="color: #0000FF;">AddParagraph</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;This is the paragraph&quot;</span>, <span style="color: #666666;">&quot;Header for Paragraph&quot;</span>, <span style="color: #666666;">&quot;MyParagraphStyle&quot;</span>, <span style="color: #666666;">&quot;MyHeaderStyle&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p><span style="font-size: small">In this post I showed you how you can use a template when creating documents and how to use custom styles in the template.<br />
In the next part I will show how you can replace content in documents using Content Controls.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetbitsandpieces.com/?feed=rss2&#038;p=83</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Document Generation with OOXML Part 1</title>
		<link>http://www.dotnetbitsandpieces.com/?p=33</link>
		<comments>http://www.dotnetbitsandpieces.com/?p=33#comments</comments>
		<pubDate>Sun, 31 Mar 2013 08:27:57 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[Document Generation]]></category>
		<category><![CDATA[OOMXL]]></category>

		<guid isPermaLink="false">http://www.dotnetbitsandpieces.com/?p=33</guid>
		<description><![CDATA[For a while I have been working on a private project called Document Generator. Document Generator is a .Net library for creating text documents in OOXML and in ODF. With the library you can create an outline for a document with paragraphs, image, etc and generate an OOXML or ODF document. I recently thought it [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><span style="font-size: small">For a while I have been working on a private project called Document Generator. Document Generator is a .Net library for creating text documents in OOXML and in ODF. With the library you can create an outline for a document with paragraphs, image, etc and generate an OOXML or ODF document. I recently thought it was mature enough to make it a final release. You can find the project at <a title="here" href="http://docgenerator.codeplex.com/" target="_blank">Codeplex </a>.</span></p>
<p><span style="font-size: small">To explain the library I decided to write a serie of articles and this is the first part. It will explain how to get started with the library.</span></p>
<h2>Pre-requisites</h2>
<p><span style="font-size: small">The library is using OOXML SDK 2.5 for creating OOXML documents and this requires .Net Framework 4.0.</span></p>
<h2>Installation</h2>
<p><span style="font-size: small">Document generator does not require any installation. Download the <a href="http://docgenerator.codeplex.com/releases/view/99752" target="_blank">zip</a> file with the latest version and extract the contents to a directory of choice. The zip file contains:</span></p>
<ul><span style="font-size: small"></p>
<li>DocumentGenerator.dll</li>
<li>DocumentFormat.OpenXml.dll (<em>This is the dll for the OOXML SDK 2.5</em>)</li>
<li>ICSharpCode.SharpZipLib.dll (<em>This dll is required for creating ODF files</em>)</li>
<p></span>
</ul>
<h2>Setup the project</h2>
<ol>
<span style="font-size: small"></p>
<li>Create a new console project in Visual Studio</li>
<li>Add a reference to the dll’s for the document generation:
<ul>
<li>DocumentGenerator.dll</li>
<li>DocumentFormat.OpenXml.dll</li>
<li>ICSharpCode.SharpZipLib.dll</li>
</ul>
</li>
<p></span>
</ol>
<h2> Generate Documents</h2>
<ol>
<span style="font-size: small"></p>
<li>The first step in generating a document is to create a Textdocument object. This is the container of the document to generate and is the same for OOXML and ODF documents. For OOXML documents it is also possible to start from a template (.dotx) or an existing document (.docx).</li>
<div class="codecolorer-container csharp geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-style: italic;">//Create a new text document</span><br />
var document <span style="color: #008000;">=</span> TextDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">//Create a new text document with a file name (including the file location)</span><br />
<span style="color: #008080; font-style: italic;">//The Boolean indicates whether the file can be overridden if a file</span><br />
<span style="color: #008080; font-style: italic;">//with the same name is found.</span><br />
var document <span style="color: #008000;">=</span> TextDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;MyFilename&quot;</span>, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">//Create a new text document based on a template</span><br />
var document <span style="color: #008000;">=</span> TextDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;MyFilename&quot;</span>,<span style="color: #666666;">&quot;myTemplate&quot;</span>, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">//Open an existing ooxml document.</span><br />
var document <span style="color: #008000;">=</span> TextDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Open</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;MyFile&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<li>When the text document is created headers and paragraphs can be added.</li>
<div class="codecolorer-container csharp geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-style: italic;">//Add a paragraph to the document. Level is an integer used for the style.</span><br />
<span style="color: #008080; font-style: italic;">//of the paragraph, e.g. level &quot;0&quot; set style &quot;Normal&quot; </span><br />
<span style="color: #008080; font-style: italic;">//and level &quot;1&quot; style &quot;Normal1&quot;</span><br />
document<span style="color: #008000;">.</span><span style="color: #0000FF;">AddParagraph</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;The paragraph text&quot;</span>, level<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">//Add a paragraph and a header. Level is an integer is used for the style.</span><br />
<span style="color: #008080; font-style: italic;">//E.g. level &quot;1&quot; applies &quot;Header1&quot; level &quot;2&quot; applies &quot;Header2&quot;</span><br />
document<span style="color: #008000;">.</span><span style="color: #0000FF;">AddParagraph</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;The header&quot;</span>,<span style="color: #666666;">&quot;The paragraph text&quot;</span>, level<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">//Add only a header. Leaving the text for the paragraph empty</span><br />
<span style="color: #008080; font-style: italic;">//will add only a header.</span><br />
document<span style="color: #008000;">.</span><span style="color: #0000FF;">AddParagraph</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;The header&quot;</span>,<span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span>, level<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<li>Finally the document can be save in OOXML or ODF format.</li>
<div class="codecolorer-container csharp geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-style: italic;">//First set a filename (including the file location), if not already set</span><br />
document<span style="color: #008000;">.</span><span style="color: #0000FF;">Filename</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;MyFile&quot;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">//Save the document in OOXML format.</span><br />
document<span style="color: #008000;">.</span><span style="color: #0000FF;">Save</span><span style="color: #008000;">&#40;</span>DocumentType<span style="color: #008000;">.</span><span style="color: #0000FF;">OOXMLTextDocument</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">//Save the document in ODF format.</span><br />
document<span style="color: #008000;">.</span><span style="color: #0000FF;">Save</span><span style="color: #008000;">&#40;</span>DocumentType<span style="color: #008000;">.</span><span style="color: #0000FF;">ODFTextDocument</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p></span>
</ol>
<p><span style="font-size: small">In this post I showed you how you can create a document, add contents to the document and save this document in either OOXML or ODF format.<br />
I also showed how you can use a dotx template or an existing ooxml document to create new documents.</span></p>
<p><span style="font-size: small">In the next post I will go through some more advanced features such as creating tables and lists and replace content in OOXML documents.<br />
If you cannot wait for these posts you can have a look a the DocumentGeneratorTest project in Codeplex to see the use of these features.<br />
</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetbitsandpieces.com/?feed=rss2&#038;p=33</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
		<item>
		<title>Enum.HasFlag() versus Bitwise comparison</title>
		<link>http://www.dotnetbitsandpieces.com/?p=11</link>
		<comments>http://www.dotnetbitsandpieces.com/?p=11#comments</comments>
		<pubDate>Mon, 14 Jan 2013 21:15:31 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Enum]]></category>

		<guid isPermaLink="false">http://www.dotnetbitsandpieces.com/?p=11</guid>
		<description><![CDATA[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 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><span style="font-size: small;">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 </span><a href="http://geekswithblogs.net/BlackRabbitCoder/archive/2010/07/22/c-fundamentals-combining-enum-values-with-bit-flags.aspx" target="_blank"><span style="font-size: small;">this blog</span></a><span style="font-size: small;">. </span></p>
<p><span style="font-size: small;">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 </span><a href="http://www.codeproject.com/Tips/441086/NETs-Enum-HasFlag-and-performance-costs" target="_blank"><span style="font-size: small;">article</span></a><span style="font-size: small;">. 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.</span></p>
<p><span style="font-size: small;">I therefor did my own test to see what the performance overhead would be and if it was worth to change.<span id="more-11"></span></span></p>
<p><span style="font-size: small;">The first part was to create an enumeration</span></p>
<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 enum EnumValues<br />
{<br />
&nbsp; Value1 = 1,<br />
&nbsp; Value2 = 2,<br />
&nbsp; ...,<br />
&nbsp; Value16 = 32768<br />
}</div></div>

</pre>
<p><span style="font-size: small;">Next I needed a class to evaluate the different comparison methods</span></p>
<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 class EnumsClass<br />
{<br />
&nbsp; public bool CheckEnum(EnumValues values)<br />
&nbsp; {<br />
&nbsp; &nbsp; bool isTrue = false;<br />
&nbsp; &nbsp; if ((values &amp;amp; EnumValues.Value8) == EnumValues.Value8) isTrue = true;<br />
&nbsp; &nbsp; return isTrue;<br />
&nbsp; }<br />
<br />
&nbsp; public bool CheckEnumWithFlagMethod(EnumValues values)<br />
&nbsp; {<br />
&nbsp; &nbsp; bool isTrue = false;<br />
&nbsp; &nbsp; if (values.HasFlag(EnumValues.Value8)) isTrue = true;<br />
&nbsp; &nbsp; return isTrue;<br />
&nbsp; }<br />
}</div></div>

</pre>
<p><span style="font-size: small;">The last part I needed was a class to call these two methods and determine how long each of the methods would take to run. I decided to use the unit test in Visual Studio 2012 as this also shows how long each test method takes.<br />
</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">[TestClass]<br />
public class EnumTest<br />
{<br />
&nbsp; private EnumsClass _enumsClass;<br />
<br />
&nbsp; [TestInitialize]<br />
&nbsp; public void Initialize()<br />
&nbsp; {<br />
&nbsp; &nbsp; _enumsClass = new EnumsClass();<br />
&nbsp; }<br />
<br />
&nbsp; [TestMethod]<br />
&nbsp; public void TestForFalseValueWithFlag()<br />
&nbsp; {<br />
&nbsp; &nbsp; Assert.IsFalse(_enumsClass.CheckEnumWithFlagMethod(EnumValues.Value1 | EnumValues.Value10));<br />
&nbsp; }<br />
<br />
&nbsp; [TestMethod]<br />
&nbsp; public void TestForFalseValue()<br />
&nbsp; {<br />
&nbsp; &nbsp; Assert.IsFalse(_enumsClass.CheckEnum(EnumValues.Value1 | EnumValues.Value10));<br />
&nbsp; }<br />
<br />
&nbsp; [TestMethod]<br />
&nbsp; public void TestForTrueValue()<br />
&nbsp; {<br />
&nbsp; &nbsp; Assert.IsTrue(_enumsClass.CheckEnum(EnumValues.Value8 | EnumValues.Value10));<br />
&nbsp; }<br />
<br />
&nbsp; [TestMethod]<br />
&nbsp; public void TestForTrueValueWithFlag()<br />
&nbsp; {<br />
&nbsp; &nbsp; Assert.IsTrue(_enumsClass.CheckEnumWithFlagMethod(EnumValues.Value8 | EnumValues.Value10));<br />
&nbsp; }<br />
}</div></div>

</pre>
<p><span style="font-size: small;">To make sure that there was no difference between a true and false result I used both methods in the EnumsClass to return both <em>true</em> and <em>false. </em>I also repeated the different calls multiple times in a foreach loop. The results are in the below.</span></p>
<table width="400" border="0" cellspacing="0" cellpadding="2">
<tbody>
<tr>
<td valign="top" width="200">Test</td>
<td valign="top" width="200">1x</td>
<td valign="top" width="200">10000x</td>
<td valign="top" width="200">100000x</td>
<td valign="top" width="200">1000000x</td>
<td valign="top" width="200">10000000x</td>
</tr>
<tr>
<td valign="top" width="200">TestForTrueValue</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">7 ms</td>
<td valign="top" width="200">79 ms</td>
</tr>
<tr>
<td valign="top" width="200">TestForTrueValueWithFlag</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">4 ms</td>
<td valign="top" width="200">45 ms</td>
<td valign="top" width="200">453 ms</td>
</tr>
<tr>
<td valign="top" width="200">TestForFalseValue</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">6 ms</td>
<td valign="top" width="200">78 ms</td>
</tr>
<tr>
<td valign="top" width="200">TestForFalseValueWithFlag</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">5 ms</td>
<td valign="top" width="200">46 ms</td>
<td valign="top" width="200">477 ms</td>
</tr>
</tbody>
</table>
<p><span style="font-size: small;">So, there is an performance cost to using the HasFlag() method. It seems that the HasFlag() method is about 6 times as slow as the good old bitwise comparison. However you will notice the difference once you need to execute the call over 100000 times.</span><br />
<span style="font-size: small;">It is up to the programmer to see if the performance cost is worth the readability. I will stick to the bitwise comparison for now.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetbitsandpieces.com/?feed=rss2&#038;p=11</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Just another blog</title>
		<link>http://www.dotnetbitsandpieces.com/?p=6</link>
		<comments>http://www.dotnetbitsandpieces.com/?p=6#comments</comments>
		<pubDate>Sat, 12 Jan 2013 15:50:29 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[Geen categorie]]></category>
		<category><![CDATA[.Net]]></category>

		<guid isPermaLink="false">http://www.dotnetbitsandpieces.com/?p=6</guid>
		<description><![CDATA[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 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>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.</p>
<p>Because of my work most post will probably be about Visual Studio and C#, but we will see when time goes by.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetbitsandpieces.com/?feed=rss2&#038;p=6</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
