111,092
社区成员




private Color m_BackgroundColor;
public Bitmap TransparentBackground(Bitmap bmp)
{
if (bmp == null)
return null;
Int32 width = bmp.Width;
Int32 height = bmp.Height;
Bitmap result = new Bitmap(width, height);
m_BackgroundColor = bmp.GetPixel(0, 0);
for (Int32 y = 0; y < height; y++)
for (Int32 x = 0; x < width; x++)
{
if (TestIfColorBelongsToBackground(bmp, x, y))
result.SetPixel(x, y, Color.Transparent);
else
result.SetPixel(x, y, bmp.GetPixel(x, y));
}
return result;
}
// This is the method need to be optimized
// Currently the border of people will contains
// the background color
private Boolean TestIfColorBelongsToBackground(Bitmap bmp, Int32 x, Int32 y)
{
if (bmp == null)
return false;
Int32 width = bmp.Width;
Int32 height = bmp.Height;
Color cp = bmp.GetPixel(x, y);
Color cp0;
Color cp1;
Color cp2;
Color cp3;
Color cp4;
Color cp5;
Color cp6;
Color cp7;
// p0 p1 p2
// p3 p p4
// p5 p6 p7
// cp0 = bmp.GetPixel(x - 1, y - 1);
// cp1 = bmp.GetPixel(x, y - 1);
// cp2 = bmp.GetPixel(x + 1, y - 1);
// cp3 = bmp.GetPixel(x - 1, y);
// cp4 = bmp.GetPixel(x + 1, y);
// cp5 = bmp.GetPixel(x - 1, y + 1);
// cp6 = bmp.GetPixel(x, y + 1);
// cp7 = bmp.GetPixel(x + 1, y + 1);
if (x == 0 && y == 0)
{
cp4 = bmp.GetPixel(x + 1, y);
cp6 = bmp.GetPixel(x, y + 1);
cp7 = bmp.GetPixel(x + 1, y + 1);
return (cp == cp4 && cp == cp6 && cp == cp7 && cp == m_BackgroundColor);
}
else if (x == 0 && y == height - 1)
{
cp1 = bmp.GetPixel(x, y - 1);
cp2 = bmp.GetPixel(x + 1, y - 1);
cp4 = bmp.GetPixel(x + 1, y);
return (cp == cp1 && cp == cp2 && cp == cp4 && cp == m_BackgroundColor);
}
else if (x == width - 1 && y == 0)
{
cp3 = bmp.GetPixel(x - 1, y);
cp5 = bmp.GetPixel(x - 1, y + 1);
cp6 = bmp.GetPixel(x, y + 1);
return (cp == cp3 && cp == cp5 && cp == cp6 && cp == m_BackgroundColor);
}
else if (x == width - 1 && y == height - 1)
{
cp0 = bmp.GetPixel(x - 1, y - 1);
cp1 = bmp.GetPixel(x, y - 1);
cp3 = bmp.GetPixel(x - 1, y);
return (cp == cp0 && cp == cp1 && cp == cp3 && cp == m_BackgroundColor);
}
else if (x != 0 && x != width - 1 && y == 0)
{
cp3 = bmp.GetPixel(x - 1, y);
cp4 = bmp.GetPixel(x + 1, y);
cp5 = bmp.GetPixel(x - 1, y + 1);
cp6 = bmp.GetPixel(x, y + 1);
cp7 = bmp.GetPixel(x + 1, y + 1);
return (cp == cp3 && cp == cp4 && cp == cp5 && cp == cp6 && cp == cp7 && cp == m_BackgroundColor);
}
else if (x != 0 && x != width - 1 && y == height - 1)
{
cp0 = bmp.GetPixel(x - 1, y - 1);
cp1 = bmp.GetPixel(x, y - 1);
cp2 = bmp.GetPixel(x + 1, y - 1);
cp3 = bmp.GetPixel(x - 1, y);
cp4 = bmp.GetPixel(x + 1, y);
return (cp == cp0 && cp == cp1 && cp == cp2 && cp == cp3 && cp == cp4 && cp == m_BackgroundColor);
}
else if (x == 0 && y != 0 && y != height - 1)
{
cp1 = bmp.GetPixel(x, y - 1);
cp2 = bmp.GetPixel(x + 1, y - 1);
cp4 = bmp.GetPixel(x + 1, y);
cp6 = bmp.GetPixel(x, y + 1);
cp7 = bmp.GetPixel(x + 1, y + 1);
return (cp == cp1 && cp == cp2 && cp == cp4 && cp == cp6 && cp == cp7 && cp == m_BackgroundColor);
}
else if (x == width - 1 && y != 0 && y != height - 1)
{
cp0 = bmp.GetPixel(x - 1, y - 1);
cp1 = bmp.GetPixel(x, y - 1);
cp3 = bmp.GetPixel(x - 1, y);
cp5 = bmp.GetPixel(x - 1, y + 1);
cp6 = bmp.GetPixel(x, y + 1);
return (cp == cp0 && cp == cp1 && cp == cp3 && cp == cp5 && cp == cp6 && cp == m_BackgroundColor);
}
else
{
cp0 = bmp.GetPixel(x - 1, y - 1);
cp1 = bmp.GetPixel(x, y - 1);
cp2 = bmp.GetPixel(x + 1, y - 1);
cp3 = bmp.GetPixel(x - 1, y);
cp4 = bmp.GetPixel(x + 1, y);
cp5 = bmp.GetPixel(x - 1, y + 1);
cp6 = bmp.GetPixel(x, y + 1);
cp7 = bmp.GetPixel(x + 1, y + 1);
return (cp == cp0 && cp == cp1 && cp == cp2 && cp == cp3 && cp == cp4 && cp == cp5 && cp == cp6 && cp == cp7 && cp == m_BackgroundColor);
}
}