private static Bitmap Crop(Bitmap b)
{
int top = 0;
int bottom = b.Height - 1;
int left = 0;
int right = b.Width - 1;
bool stop = false;
for (int y = 0; y < b.Height && !stop; y++)
{
for (int x = 0; x < b.Width && !stop; x++)
{
if (b.GetPixel(x, y).A > 0)
{
top = y;
stop = true;
break;
}
}
}
stop = false;
for (int y = b.Height - 1; y > top && !stop; y--)
{
for (int x = 0; x < b.Width && !stop; x++)
{
if (b.GetPixel(x, y).A > 0)
{
bottom = y + 1;
stop = true;
break;
}
}
}
stop = false;
for (int x = 0; x < b.Width && !stop; x++)
{
for(int y = 0; y < b.Height && !stop; y++)
{
if (b.GetPixel(x, y).A > 0)
{
left = x;
stop = true;
break;
}
}
}
stop = false;
for (int x = b.Width - 1; x > left && !stop; x--)
{
for (int y = 0; y < b.Height && !stop; y++)
{
if (b.GetPixel(x, y).A > 0)
{
right = x + 1;
stop = true;
break;
}
}
}
Bitmap ret = b.Clone(Rectangle.FromLTRB(left, top, right, bottom), b.PixelFormat);
b.Dispose();
return ret;
}