Image Dilation in C#

It's like going to the optometrist for your pictures or something?
Jan 23 2009 by James Craig

This is probably not the most useful image manipulation function that you will ever see but it's fun (well if you're a programmer that enjoys screwing with people, it can be fun anyway). Anyway, image dilation is simply the taking of high intensity pixels (usually foreground pixels but not always) and grow the size of their boundaries. In other words we bleed out wards, causing bright colors to blur. It can be used for a number of effects, filling in of small spots in images, etc. For example, whenever you see a white screen (as though you're looking into direct bright light) and suddenly you see a dark blurry figure that slowly comes into focus, they're using this effect (in conjunction with a couple others).

The way this filter works is it takes a pixel (p0) and looks at a number of pixels around it (the shape of this can be anything but is usually a box around the center pixel). It then finds the pixel within that group with the highest intensity (in black and white images, the value closest to white and in color images you can break it down by red, green, and blue values) and changes the value of the pixel (p0) to that value. It does this for the entire image, causing the brighter values to bleed out.
Anyway, as always I wrote some code to accomplish this and figured I'd share:

 public static Bitmap Dilate(Bitmap Image, int Size)
{
System.Drawing.Bitmap TempBitmap = Image;
System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height);
System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap);
NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), System.Drawing.GraphicsUnit.Pixel);
NewGraphics.Dispose();
Random TempRandom = new Random();
int ApetureMin = -(Size / 2);
int ApetureMax = (Size / 2);
for (int x = 0; x < NewBitmap.Width; ++x)
{
for (int y = 0; y < NewBitmap.Height; ++y)
{
int RValue = 0;
int GValue = 0;
int BValue = 0;
for (int x2 = ApetureMin; x2 < ApetureMax; ++x2)
{
int TempX = x + x2;
if (TempX >= 0 && TempX < NewBitmap.Width)
{
for (int y2 = ApetureMin; y2 < ApetureMax; ++y2)
{
int TempY = y + y2;
if (TempY >= 0 && TempY < NewBitmap.Height)
{
Color TempColor = TempBitmap.GetPixel(TempX, TempY);
if (TempColor.R > RValue)
RValue = TempColor.R;
if (TempColor.G > GValue)
GValue = TempColor.G;
if (TempColor.B > BValue)
BValue = TempColor.B;
}
}
}
}
Color TempPixel = Color.FromArgb(RValue, GValue, BValue);
NewBitmap.SetPixel(x, y, TempPixel);
}
}
return NewBitmap;
}

The code above takes in an image and the size of the box (I haven't written code to take in a defined box structure but it can be changed to do so). It then returns a new Bitmap object with the dilation completed. Note that there is most likely an updated version within my utility library, so you may want to go and download it there.