<?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>Eric Willis&#039; Notes &#187; .net</title>
	<atom:link href="http://notes.ericwillis.com/tags/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://notes.ericwillis.com</link>
	<description>Time traveler and engineer</description>
	<lastBuildDate>Thu, 22 Jul 2010 01:07:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Fake Latin Generator in ASP.NET MVC</title>
		<link>http://notes.ericwillis.com/2010/07/fake-latin-generator-in-asp-net-mvc/</link>
		<comments>http://notes.ericwillis.com/2010/07/fake-latin-generator-in-asp-net-mvc/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 01:07:40 +0000</pubDate>
		<dc:creator>Eric Willis</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[mvc]]></category>

		<guid isPermaLink="false">http://notes.ericwillis.com/?p=58</guid>
		<description><![CDATA[While designing HTML templates, I, like everyone else, frequently use filler text to fill up space before content exists. I&#8217;ve been doing this a lot lately in ASP.NET MVC 2. Having static fake Latin filler text in a web application while developing is nice but having random fake Latin text is more useful for a [...]]]></description>
			<content:encoded><![CDATA[<p>While designing HTML templates, I, like everyone else, frequently use filler text to fill up space before content exists. I&#8217;ve been doing this a lot lately in <a href="http://www.asp.net/mvc">ASP.NET MVC 2</a>. Having static fake Latin filler text in a web application while developing is nice but having random fake Latin text is more useful for a couple of reasons:</p>
<ol>
<li>It forces your page to render with different lengths of text which can help find bugs</li>
<li>It&#8217;s more amusing and beats staring at the exact same text</li>
</ol>
<p>Since I&#8217;m working in ASP.NET MVC mostly, I&#8217;ve used C# to create several <a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx">extension methods</a> on the <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.htmlhelper.aspx">HtmlHelper</a> object. This enables me to generate fake Latin text inline in HTML and Views.</p>
<pre class="brush: xml; title: ; notranslate">
&lt;h1&gt;&lt;%= Html.FakeLatinTitle(5) %&gt;&lt;/h1&gt;
&lt;p&gt;
    &lt;%= Html.FakeLatinParagraph(500) %&gt;
&lt;/p&gt;
</pre>
<p>Here&#8217;s the C# source behind FakeLatinParagraph(), FakeLatinParagraphs() and FakeLatinTitle():</p>
<pre class="brush: csharp; title: ; notranslate">
using System;
using System.Web.Mvc;
using System.Text;

namespace Web.Extensions
{
    public static class HtmlHelperExtensions
    {
        #region Private Statics

        private static int _seed;

        #endregion

        #region Extension Methods

        public static string FakeLatinParagraph(this HtmlHelper helper, int wordCount)
        {
            string[] dictionary = &quot;lorem ipsum dolor sit amet consectetuer adipiscing elit sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat ut wisi enim ad minim veniam quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi Ut wisi enim ad minim veniam quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi&quot;.Split(' ');
            StringBuilder words = new StringBuilder();
            Random rand = null;
            string paragraph = string.Empty;

            if (HtmlHelperExtensions._seed &gt;= int.MaxValue - 1000)
                HtmlHelperExtensions._seed = 0;

            rand = new Random(HtmlHelperExtensions._seed += DateTime.Now.Millisecond);

            for (int n = 0; n &lt; wordCount; n++)
                words.AppendFormat(&quot;{0} &quot;, dictionary[rand.Next(0, dictionary.Length)]);

            paragraph = words.ToString();
            paragraph = string.Format(&quot;{0}{1}.&quot;, char.ToUpperInvariant(paragraph[0]), paragraph.Substring(1).TrimEnd());

            return paragraph;
        }

        public static string FakeLatinParagraphs(this HtmlHelper helper, int paragraphCount, int wordsPerParagraph, string beforeParagraph, string afterParagraph)
        {
            StringBuilder paragraphs = new StringBuilder();

            for (int n = 0; n &lt; paragraphCount; n++)
            {
                paragraphs.AppendFormat(&quot;\n{0}\n&quot;, beforeParagraph);
                paragraphs.AppendFormat(&quot;\t{0}&quot;, HtmlHelperExtensions.FakeLatinParagraph(helper, wordsPerParagraph));
                paragraphs.AppendFormat(&quot;\n{0}\n&quot;, afterParagraph);
            }

            return paragraphs.ToString();
        }

        public static string FakeLatinTitle(this HtmlHelper helper, int wordCount)
        {
            string title = HtmlHelperExtensions.FakeLatinParagraph(helper, wordCount);

            title = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(title);
            title = title.Substring(0, title.Length - 1); // kill period from paragraph
            return title;
        }

        #endregion
    }
}
</pre>
<p>Don&#8217;t forget to include the namespace where the extension methods exist anywhere where you want to have access to them.</p>
<pre class="brush: csharp; title: ; notranslate">
using System;
using System.Web;
using System.Web.Mvc;
// ... whatever other namespaces
using Web.Extensions; // YOU NEED ME!
</pre>
<p>If you want a set of extension methods to be available on every ASP.NET MVC View, you can add the reference in the <a href="http://msdn.microsoft.com/en-us/library/aa719558(VS.71).aspx">web.config</a> file.</p>
<pre class="brush: xml; title: ; notranslate">
&lt;!-- SNIPPET OF WEB CONFIG --&gt;
&lt;pages controlRenderingCompatibilityVersion=&quot;3.5&quot; clientIDMode=&quot;AutoID&quot;&gt;
    &lt;namespaces&gt;
        &lt;add namespace=&quot;System.Web.Mvc&quot;/&gt;
        &lt;add namespace=&quot;System.Web.Mvc.Ajax&quot;/&gt;
        &lt;add namespace=&quot;System.Web.Mvc.Html&quot;/&gt;
        &lt;add namespace=&quot;System.Web.Routing&quot;/&gt;
        &lt;add namespace=&quot;System.Linq&quot;/&gt;
        &lt;add namespace=&quot;System.Collections.Generic&quot;/&gt;
        &lt;add namespace=&quot;Web.Extensions&quot;/&gt; &lt;!-- EXTENSION METHODS NAMESPACE --&gt;
    &lt;/namespaces&gt;
&lt;/pages&gt;
&lt;!-- END SNIPPET OF WEB CONFIG (don't replace your entire web.config file with only this) --&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://notes.ericwillis.com/2010/07/fake-latin-generator-in-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Make an image black and white in c#</title>
		<link>http://notes.ericwillis.com/2009/11/make-an-image-black-and-white-in-csharp/</link>
		<comments>http://notes.ericwillis.com/2009/11/make-an-image-black-and-white-in-csharp/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 06:09:51 +0000</pubDate>
		<dc:creator>Eric Willis</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[blackandwhite]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[imaging]]></category>

		<guid isPermaLink="false">http://ericwillis.com/notes/?p=20</guid>
		<description><![CDATA[I&#8217;ve gone on a strange image manipulation kick in c# recently. Partly because I needed blurring method for something (then pixelation spawned from that) and then partially because it was fun. Here&#8217;s how to remove color from an image in c#: The BlackAndWhite() method takes a look at every pixel in the region and averages [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve gone on a strange image manipulation kick in c# recently. Partly because I needed <a href="http://ericwillis.com/notes/2009/10/blur-an-image-with-csharp/">blurring</a> method for something (then <a href="http://ericwillis.com/notes/2009/11/pixelate-an-image-with-csharp/">pixelation</a> spawned from that) and then partially because it was fun. Here&#8217;s how to remove color from an image in c#:</p>
<pre class="brush: csharp; title: ; notranslate">
private static Bitmap BlackAndWhite(Bitmap image, Rectangle rectangle)
{
    Bitmap blackAndWhite = new System.Drawing.Bitmap(image.Width, image.Height);

    // make an exact copy of the bitmap provided
    using(Graphics graphics = System.Drawing.Graphics.FromImage(blackAndWhite))
        graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
            new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);

    // for every pixel in the rectangle region
    for (Int32 xx = rectangle.X; xx &lt; rectangle.X + rectangle.Width &amp;&amp; xx &lt; image.Width; xx++)
    {
        for (Int32 yy = rectangle.Y; yy &lt; rectangle.Y + rectangle.Height &amp;&amp; yy &lt; image.Height; yy++)
        {
            // average the red, green and blue of the pixel to get a gray value
            Color pixel = blackAndWhite.GetPixel(xx, yy);
            Int32 avg = (pixel.R + pixel.G + pixel.B) / 3;

            blackAndWhite.SetPixel(xx, yy, Color.FromArgb(0, avg, avg, avg));
        }
    }

    return blackAndWhite;
}
</pre>
<p>The BlackAndWhite() method takes a look at every pixel in the region and averages the red, green and blue values to get a gray value. Once a gray value is determined, the pixel is set to that color.</p>
<div id="attachment_21" class="wp-caption alignnone" style="width: 510px"><img src="http://notes.ericwillis.com/wp-content/uploads/2009/11/joes.jpg" alt="Joe&#039;s Crab Shack in San Francisco" title="Joe&#039;s Crab Shack in San Francisco" width="500" height="333" class="size-full wp-image-21" /><p class="wp-caption-text">Joe's Crab Shack in San Francisco</p></div>
<div id="attachment_22" class="wp-caption alignnone" style="width: 510px"><img src="http://notes.ericwillis.com/wp-content/uploads/2009/11/joes-bw.jpg" alt="Black and White Joe&#039;s Crab Shack in San Francisco" title="Black and White Joe&#039;s Crab Shack in San Francisco" width="500" height="333" class="size-full wp-image-22" /><p class="wp-caption-text">Black and White Joe's Crab Shack in San Francisco</p></div>
<p>If you want to make the entire image black and white and not just a region (rectangle), then overload the BlackAndWhite() method like this:</p>
<pre class="brush: csharp; title: ; notranslate">
private static Bitmap BlackAndWhite(Bitmap image)
{
    return BlackAndWhite(image, new Rectangle(0, 0, image.Width, image.Height));
}
</pre>
<p><em>Download the original <a href="http://www.flickr.com/photos/superic/2138322245">Joe&#8217;s Crab Shack image</a>.</em></p>
<p>*Update: I was poking around on the Internet, looking at things and stuff and found a better greyscale implementation (read: faster, much faster and a bit more complex) from <a href="http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale">Switch On The Code</a>. Check it out:</p>
<pre class="brush: csharp; title: ; notranslate">
public static Bitmap MakeGrayscale3(Bitmap original)
{
   //create a blank bitmap the same size as original
   Bitmap newBitmap = new Bitmap(original.Width, original.Height);

   //get a graphics object from the new image
   Graphics g = Graphics.FromImage(newBitmap);

   //create the grayscale ColorMatrix
   ColorMatrix colorMatrix = new ColorMatrix(
      new float[][]
      {
         new float[] {.3f, .3f, .3f, 0, 0},
         new float[] {.59f, .59f, .59f, 0, 0},
         new float[] {.11f, .11f, .11f, 0, 0},
         new float[] {0, 0, 0, 1, 0},
         new float[] {0, 0, 0, 0, 1}
      });

   //create some image attributes
   ImageAttributes attributes = new ImageAttributes();

   //set the color matrix attribute
   attributes.SetColorMatrix(colorMatrix);

   //draw the original image on the new image
   //using the grayscale color matrix
   g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
      0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);

   //dispose the Graphics object
   g.Dispose();
   return newBitmap;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://notes.ericwillis.com/2009/11/make-an-image-black-and-white-in-csharp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pixelate an image with c#</title>
		<link>http://notes.ericwillis.com/2009/11/pixelate-an-image-with-csharp/</link>
		<comments>http://notes.ericwillis.com/2009/11/pixelate-an-image-with-csharp/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 04:51:46 +0000</pubDate>
		<dc:creator>Eric Willis</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[imaging]]></category>
		<category><![CDATA[pixelate]]></category>

		<guid isPermaLink="false">http://ericwillis.com/notes/?p=16</guid>
		<description><![CDATA[Pixelating an image in c# is on par with blurring an image &#8211; not terribly complex. This method accepts an image, a pixelate region (rectangle), a pixelate size and returns a bitmap. The pixelate method looks at ever block of pixels in the pixelate size, grabs the middle pixel and then sets all of the [...]]]></description>
			<content:encoded><![CDATA[<p>Pixelating an image in c# is on par with <a href="http://ericwillis.com/notes/2009/10/blur-an-image-with-csharp/">blurring an image</a> &ndash; not terribly complex. This method accepts an image, a pixelate region (rectangle), a pixelate size and returns a bitmap.</p>
<pre class="brush: csharp; title: ; notranslate">
private static Bitmap Pixelate(Bitmap image, Rectangle rectangle, Int32 pixelateSize)
{
    Bitmap pixelated = new System.Drawing.Bitmap(image.Width, image.Height);

    // make an exact copy of the bitmap provided
    using (Graphics graphics = System.Drawing.Graphics.FromImage(pixelated))
        graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
            new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);

    // look at every pixel in the rectangle while making sure we're within the image bounds
    for (Int32 xx = rectangle.X; xx &lt; rectangle.X + rectangle.Width &amp;&amp; xx &lt; image.Width; xx += pixelateSize)
    {
        for (Int32 yy = rectangle.Y; yy &lt; rectangle.Y + rectangle.Height &amp;&amp; yy &lt; image.Height; yy += pixelateSize)
        {
            Int32 offsetX = pixelateSize / 2;
            Int32 offsetY = pixelateSize / 2;

            // make sure that the offset is within the boundry of the image
            while (xx + offsetX &gt;= image.Width) offsetX--;
            while (yy + offsetY &gt;= image.Height) offsetY--;

            // get the pixel color in the center of the soon to be pixelated area
            Color pixel = pixelated.GetPixel(xx + offsetX, yy + offsetY);

            // for each pixel in the pixelate size, set it to the center color
            for (Int32 x = xx; x &lt; xx + pixelateSize &amp;&amp; x &lt; image.Width; x++)
                for (Int32 y = yy; y &lt; yy + pixelateSize &amp;&amp; y &lt; image.Height; y++)
                    pixelated.SetPixel(x, y, pixel);
        }
    }

    return pixelated;
}
</pre>
<p>The pixelate method looks at ever block of pixels in the pixelate size, grabs the middle pixel and then sets all of the pixels in the block to that same color. If you don&#8217;t use the middle pixel, the image ends up shifting (my initial version looked at the top-left pixel.) </p>
<div id="attachment_17" class="wp-caption alignnone" style="width: 343px"><img src="http://notes.ericwillis.com/wp-content/uploads/2009/11/dad.jpg" alt="Dad at Christmas" title="Dad at Christmas" width="333" height="500" class="size-full wp-image-17" /><p class="wp-caption-text">Dad at Christmas</p></div>
<div id="attachment_18" class="wp-caption alignnone" style="width: 343px"><img src="http://notes.ericwillis.com/wp-content/uploads/2009/11/dad-pixelate.jpg" alt="Pixelated Dad at Christmas" title="Pixelated Dad at Christmas" width="333" height="500" class="size-full wp-image-18" /><p class="wp-caption-text">Pixelated Dad at Christmas</p></div>
<p>If you want to pixelate the whole image, just use the bounds of the original image as the pixelate rectangle. Here’s an example of how to overload the Pixelate() method:</p>
<pre class="brush: csharp; title: ; notranslate">
private static Bitmap Pixelate(Bitmap image, Int32 blurSize)
{
    return Pixelate(image, new Rectangle(0, 0, image.Width, image.Height), blurSize);
}
</pre>
<p><em>Download original <a href="http://www.flickr.com/photos/superic/3377785931">dad image</a>.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://notes.ericwillis.com/2009/11/pixelate-an-image-with-csharp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Blur an image with c#</title>
		<link>http://notes.ericwillis.com/2009/10/blur-an-image-with-csharp/</link>
		<comments>http://notes.ericwillis.com/2009/10/blur-an-image-with-csharp/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 06:08:57 +0000</pubDate>
		<dc:creator>Eric Willis</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[blur]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[imaging]]></category>

		<guid isPermaLink="false">http://ericwillis.com/notes/?p=6</guid>
		<description><![CDATA[Blurring an image with c# is pretty simple. I was messing around with something one night and needed to blur certain regions of JPGs that I was processing. This method accepts an image, a blur region (rectangle), a blur size and returns a blurred bitmap. The method takes a look at every pixel within the [...]]]></description>
			<content:encoded><![CDATA[<p>Blurring an image with c# is pretty simple. I was messing around with something one night and needed to blur certain regions of JPGs that I was processing. This method accepts an image, a blur region (rectangle), a blur size and returns a blurred bitmap.</p>
<pre class="brush: csharp; title: ; notranslate">
private static Bitmap Blur(Bitmap image, Rectangle rectangle, Int32 blurSize)
{
    Bitmap blurred = new Bitmap(image.Width, image.Height);

    // make an exact copy of the bitmap provided
    using(Graphics graphics = Graphics.FromImage(blurred))
        graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height),
            new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);

    // look at every pixel in the blur rectangle
    for (Int32 xx = rectangle.X; xx &lt; rectangle.X + rectangle.Width; xx++)
    {
        for (Int32 yy = rectangle.Y; yy &lt; rectangle.Y + rectangle.Height; yy++)
        {
            Int32 avgR = 0, avgG = 0, avgB = 0;
            Int32 blurPixelCount = 0;

            // average the color of the red, green and blue for each pixel in the
            // blur size while making sure you don't go outside the image bounds
            for (Int32 x = xx; (x &lt; xx + blurSize &amp;&amp; x &lt; image.Width); x++)
            {
                for (Int32 y = yy; (y &lt; yy + blurSize &amp;&amp; y &lt; image.Height); y++)
                {
                    Color pixel = blurred.GetPixel(x, y);

                    avgR += pixel.R;
                    avgG += pixel.G;
                    avgB += pixel.B;

                    blurPixelCount++;
                }
            }

            avgR = avgR / blurPixelCount;
            avgG = avgG / blurPixelCount;
            avgB = avgB / blurPixelCount;

            // now that we know the average for the blur size, set each pixel to that color
            for (Int32 x = xx; x &lt; xx + blurSize &amp;&amp; x &lt; image.Width &amp;&amp; x &lt; rectangle.Width; x++)
                for (Int32 y = yy; y &lt; yy + blurSize &amp;&amp; y &lt; image.Height &amp;&amp; y &lt; rectangle.Height; y++)
                    blurred.SetPixel(x, y, Color.FromArgb(avgR, avgG, avgB));
        }
    }

    return blurred;
}
</pre>
<p>The method takes a look at every pixel within the blur rectangle and samples the reds, greens and blues within the blur size to figure out an average. Then it sets every pixel within the blur size square to the average color.</p>
<div id="attachment_9" class="wp-caption alignnone" style="width: 510px"><img src="http://notes.ericwillis.com/wp-content/uploads/2009/10/jellyfish.jpg" alt="Jellyfish at the Monterey Aquarium" title="Jellyfish at the Monterey Aquarium" width="500" height="333" class="size-full wp-image-9" /><p class="wp-caption-text">Jellyfish at the Monterey Aquarium</p></div>
<div id="attachment_10" class="wp-caption alignnone" style="width: 510px"><img src="http://notes.ericwillis.com/wp-content/uploads/2009/10/jellyfish-blur.jpg" alt="Blurred jellyfish at the Monterey Aquarium" title="Blurred jellyfish at the Monterey Aquarium" width="500" height="333" class="size-full wp-image-10" /><p class="wp-caption-text">Blurred jellyfish at the Monterey Aquarium</p></div>
<p>If you want to blur the whole image, just use the bounds of the original image as the blur rectangle. Here&#8217;s an example of how to overload the Blur() method:</p>
<pre class="brush: csharp; title: ; notranslate">
private static Bitmap Blur(Bitmap image, Int32 blurSize)
{
    return Blur(image, new Rectangle(0, 0, image.Width, image.Height), blurSize);
}
</pre>
<p><em>Download original <a href="http://www.flickr.com/photos/superic/3002019512/">jellyfish image</a>.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://notes.ericwillis.com/2009/10/blur-an-image-with-csharp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

