int val = 90;

// this is the Thresold value, if we change it, output will be different & for different situation we can change it.
public Bitmap My_Edge_Detection(Bitmap b)
{
int n, m;
n = b.Height;
m = b.Width;
int cc = 0, total = 0;
Bitmap B1 = new Bitmap(m, n);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
{
B1.SetPixel(i, j, Color.White);
}

for (int i = 1; i < m – 1; i++)
for (int j = 1; j < n – 1; j++)
{
total = Math.Abs(b.GetPixel(i + 1, j + 1).B – b.GetPixel(i, j).B) + Math.Abs(b.GetPixel(i + 1, j + 1).R – b.GetPixel(i, j).R) + Math.Abs(b.GetPixel(i + 1, j + 1).G – b.GetPixel(i, j).G);
if (total > val)
{ B1.SetPixel(i, j, Color.Black); continue; }

total = Math.Abs(b.GetPixel(i, j + 1).B – b.GetPixel(i, j).B) + Math.Abs(b.GetPixel(i, j + 1).R – b.GetPixel(i, j).R) + Math.Abs(b.GetPixel(i, j + 1).G – b.GetPixel(i, j).G);
if (total > val)
{ B1.SetPixel(i, j, Color.Black); continue; }

total = Math.Abs(b.GetPixel(i + 1, j).B – b.GetPixel(i, j).B) + Math.Abs(b.GetPixel(i + 1, j).R – b.GetPixel(i, j).R) + Math.Abs(b.GetPixel(i + 1, j).G – b.GetPixel(i, j).G);
if (total > val)
{ B1.SetPixel(i, j, Color.Black); continue; }

total = Math.Abs(b.GetPixel(i – 1, j – 1).B – b.GetPixel(i, j).B) + Math.Abs(b.GetPixel(i – 1, j – 1).R – b.GetPixel(i, j).R) + Math.Abs(b.GetPixel(i – 1, j – 1).G – b.GetPixel(i, j).G);
if (total > val)
{ B1.SetPixel(i, j, Color.Black); continue; }

total = Math.Abs(b.GetPixel(i, j – 1).B – b.GetPixel(i, j).B) + Math.Abs(b.GetPixel(i, j – 1).R – b.GetPixel(i, j).R) + Math.Abs(b.GetPixel(i, j – 1).G – b.GetPixel(i, j).G);
if (total > val)
{ B1.SetPixel(i, j, Color.Black); continue; }

total = Math.Abs(b.GetPixel(i – 1, j).B – b.GetPixel(i, j).B) + Math.Abs(b.GetPixel(i – 1, j).R – b.GetPixel(i, j).R) + Math.Abs(b.GetPixel(i – 1, j).G – b.GetPixel(i, j).G);
if (total > val)
{ B1.SetPixel(i, j, Color.Black); continue; }

total = Math.Abs(b.GetPixel(i – 1, j + 1).B – b.GetPixel(i, j).B) + Math.Abs(b.GetPixel(i – 1, j + 1).R – b.GetPixel(i, j).R) + Math.Abs(b.GetPixel(i – 1, j + 1).G – b.GetPixel(i, j).G);
if (total > val)
{ B1.SetPixel(i, j, Color.Black); continue; }

total = Math.Abs(b.GetPixel(i + 1, j – 1).B – b.GetPixel(i, j).B) + Math.Abs(b.GetPixel(i + 1, j – 1).R – b.GetPixel(i, j).R) + Math.Abs(b.GetPixel(i + 1, j – 1).G – b.GetPixel(i, j).G);
if (total > val)
{ B1.SetPixel(i, j, Color.Black); continue; }

}

return B1;
}

Advertisements