<?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; blur</title>
	<atom:link href="http://notes.ericwillis.com/tags/blur/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>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>

