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

