Skip to content

Instantly share code, notes, and snippets.

@jaydp17
Created March 28, 2013 13:43
Show Gist options
  • Save jaydp17/5263207 to your computer and use it in GitHub Desktop.
Save jaydp17/5263207 to your computer and use it in GitHub Desktop.
WriteableBitmap version
public static Boundaries CheckBoundaries(WriteableBitmap bmp)
{
// check left
double boundayFactor = 0.1; //10% of the image width/height
Boundaries b = new Boundaries();
b.Left = CheckLeft(bmp, boundayFactor);
b.Right = CheckRight(bmp, boundayFactor);
b.Top = CheckTop(bmp, boundayFactor);
b.Bottom = CheckBottom(bmp, boundayFactor);
return b;
}
private static bool CheckLeft(WriteableBitmap bmp, double bf)
{
for (int i = 0; i < (int)(bmp.PixelWidth * bf); i++)
{
int count = 0;
for (int j = 0; j < bmp.PixelHeight - 1; j++)
{
if (bmp.GetPixel(i, j) != Color.FromArgb(0, 0, 0, 0))
{
count++;
}
}
if (count > 12)
return true;
}
return false;
}
private static bool CheckRight(WriteableBitmap bmp, double bf)
{
for (int i = bmp.PixelWidth - 1; i > (int)(bmp.PixelWidth * (1 - bf)); i--)
{
int count = 0;
for (int j = 0; j < bmp.PixelHeight - 1; j++)
{
if (bmp.GetPixel(i, j) != Color.FromArgb(0, 0, 0, 0))
{
count++;
}
}
if (count > 12)
return true;
}
return false;
}
private static bool CheckTop(WriteableBitmap bmp, double bf)
{
for (int i = 0; i < (int)(bmp.PixelHeight * bf); i++)
{
int count = 0;
for (int j = 0; j < bmp.PixelWidth - 1; j++)
{
if (bmp.GetPixel(j, i) != Color.FromArgb(0, 0, 0, 0))
{
count++;
}
}
if (count > 12)
return true;
}
return false;
}
private static bool CheckBottom(WriteableBitmap bmp, double bf)
{
for (int i = bmp.PixelHeight - 1; i > (int)(bmp.PixelHeight * (1 - bf)); i--)
{
int count = 0;
for (int j = 0; j < bmp.PixelWidth - 1; j++)
{
if (bmp.GetPixel(j, i) != Color.FromArgb(0, 0, 0, 0))
{
count++;
}
}
if (count > 12)
return true;
}
return false;
}
public class Boundaries
{
public bool Left { get; set; }
public bool Right { get; set; }
public bool Top { get; set; }
public bool Bottom { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment