位图无法找到自己的像素!

时间:2010-09-10 06:12:55

标签: c# graphics bitmap pixel

情景:

1)程序将在位图上绘制一个字符串(通常是单个字符):

protected void DrawCharacter(string character, Font font)
{
    if(string.IsNullOrEmpty(character))
        character = ".";

    FontFamily f = new FontFamily(FontName);            

    bitmap = new Bitmap((int)(font.Size * 2f), (int)font.Height);
    Graphics g = Graphics.FromImage(bitmap);
    g.Clear(Color.White);
    g.DrawString(character, font, Brushes.Black, DrawPoint);            
}

2)使用以下算法得到所有黑色像素位置:

        public Int16[] GetColoredPixcels(Bitmap bmp, bool useGeneric)
        {
            List<short> pixels = new List<short>();

            int x = 0, y = 0;

            do
            {
                Color c = bmp.GetPixel(x, y);
                if (c.R == 0 && c.G == 0 && c.B == 0)
                    pixels.Add((Int16)(x + (y + 1) * bmp.Width));


                if (x == bmp.Width - 1)
                {
                    x = 0;
                    y++;
                }
                else
                    x++;

            } while (y < bmp.Height);

            return pixels.ToArray();
        }

当输入字符是单个点(。)时出现问题。在处理函数bmp.GetPixel(x, y)时,我不知道位图对象中发生了什么,因为它无法找到点位置!输出数组声明位图没有黑点!但是当输入字符串是(:)程序时可以正确找到像素位置!

有任何建议或指导吗? 提前谢谢......

1 个答案:

答案 0 :(得分:2)

怀疑反锯齿意味着像素为“。”不是完全黑色。为什么不改变你的条件只选择“非常暗”的像素?

private const int Threshold = 10;
...
if (c.R < Threshold && c.G < Threshold && c.B < Threshold)