Skip to content

Instantly share code, notes, and snippets.

@superic
Created December 28, 2013 23:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superic/8165723 to your computer and use it in GitHub Desktop.
Save superic/8165723 to your computer and use it in GitHub Desktop.
Pixelate an image with c#. More information: http://eric.tumblr.com/post/71459967718/pixelate-an-image-with-c
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 < rectangle.X + rectangle.Width && xx < image.Width; xx += pixelateSize)
{
for (Int32 yy = rectangle.Y; yy < rectangle.Y + rectangle.Height && yy < 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 >= image.Width) offsetX--;
while (yy + offsetY >= 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 < xx + pixelateSize && x < image.Width; x++)
for (Int32 y = yy; y < yy + pixelateSize && y < image.Height; y++)
pixelated.SetPixel(x, y, pixel);
}
}
return pixelated;
}
private static Bitmap Pixelate(Bitmap image, Int32 blurSize)
{
return Pixelate(image, new Rectangle(0, 0, image.Width, image.Height), blurSize);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment