Skip to content

Instantly share code, notes, and snippets.

@superic
Created December 28, 2013 23:48
Show Gist options
  • Save superic/8165746 to your computer and use it in GitHub Desktop.
Save superic/8165746 to your computer and use it in GitHub Desktop.
Blur an image with c#. More information: http://eric.tumblr.com/post/71460344489/blur-an-image-with-c
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 < rectangle.X + rectangle.Width; xx++)
{
for (Int32 yy = rectangle.Y; yy < 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 < xx + blurSize && x < image.Width); x++)
{
for (Int32 y = yy; (y < yy + blurSize && y < 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 < xx + blurSize && x < image.Width && x < rectangle.Width; x++)
for (Int32 y = yy; y < yy + blurSize && y < image.Height && y < rectangle.Height; y++)
blurred.SetPixel(x, y, Color.FromArgb(avgR, avgG, avgB));
}
}
return blurred;
}
private static Bitmap Blur(Bitmap image, Int32 blurSize)
{
return Blur(image, new Rectangle(0, 0, image.Width, image.Height), blurSize);
}
@AFract
Copy link

AFract commented Jun 29, 2017

Hello,
I have seen you code in a StackOverflow conversation, and I found it was behaving weirdly. I think I found a bug inside of it, for me :
Color pixel = blurred.GetPixel(x, y);
Should be
Color pixel = image.GetPixel(x, y);
You can find the conversation here : https://stackoverflow.com/questions/44827093/how-to-apply-blur-effect-on-image/44827454#comment76636962_44827454 if you wish

@MSandeep111
Copy link

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

will be
// now that we know the average for the blur size, set each pixel to that color
for (int x = xx; x < xx + blurSize && x < image.Width && x < rectangle.X + rectangle.Width; x++)
for (int y = yy; y < yy + blurSize && y < image.Height && y < rectangle.Y + rectangle.Height; y++)
blurred.SetPixel(x, y, Color.FromArgb(avgR, avgG, avgB));

x < rectangle.Width replaced with rectangle.X + rectangle.Width
and y < rectangle.Height reaplced with rectangle.Y + rectangle.Height
to blur the any specific portion on the image as the code is working right for entire image but for part of image it amy or may not work depending on provided coordinates in rectangle in the below function.

return Blur(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