1: /// <summary>
2: /// Does dilation
3: /// </summary>
4: /// <param name="OriginalImage">Image to manipulate</param>
5: /// <param name="Size">Size of the aperture</param>
6: public static Bitmap Dilate(Bitmap OriginalImage, int Size)
7: {
8: Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
9: BitmapData NewData = Image.LockImage(NewBitmap);
10: BitmapData OldData = Image.LockImage(OriginalImage);
11: int NewPixelSize = Image.GetPixelSize(NewData);
12: int OldPixelSize = Image.GetPixelSize(OldData);
13: int ApetureMin = -(Size / 2);
14: int ApetureMax = (Size / 2);
15: for (int x = 0; x < NewBitmap.Width; ++x)
16: {
17: for (int y = 0; y < NewBitmap.Height; ++y)
18: {
19: int RValue = 0;
20: int GValue = 0;
21: int BValue = 0;
22: for (int x2 = ApetureMin; x2 < ApetureMax; ++x2)
23: {
24: int TempX = x + x2;
25: if (TempX >= 0 && TempX < NewBitmap.Width)
26: {
27: for (int y2 = ApetureMin; y2 < ApetureMax; ++y2)
28: {
29: int TempY = y + y2;
30: if (TempY >= 0 && TempY < NewBitmap.Height)
31: {
32: Color TempColor = Image.GetPixel(OldData, TempX, TempY, OldPixelSize);
33: if (TempColor.R > RValue)
34: RValue = TempColor.R;
35: if (TempColor.G > GValue)
36: GValue = TempColor.G;
37: if (TempColor.B > BValue)
38: BValue = TempColor.B;
39: }
40: }
41: }
42: }
43: Color TempPixel = Color.FromArgb(RValue, GValue, BValue);
44: Image.SetPixel(NewData, x, y, TempPixel, NewPixelSize);
45: }
46: }
47: Image.UnlockImage(NewBitmap, NewData);
48: Image.UnlockImage(OriginalImage, OldData);
49: return NewBitmap;
50: }