views:

38

answers:

1

Scenario:

1) Program going to draw a string (commonly a single character) on a bitmap:

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) Using following algorithm we get all black pixels position:

        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();
        }

Problem occurs when input character is a single point (.). I don't know what's happening in bitmap object while processing function bmp.GetPixel(x, y), because it can't find point position! Output array claims bitmap has no black point! But when input string is (:) program can find pixels position properly!

Any suggestion or guid? thanks in advance...

+1  A: 

I suspect that anti-aliasing means that the pixel for "." isn't completely black. Why not change your condition to just pick "very dark" pixels?

private const int Threshold = 10;
...
if (c.R < Threshold && c.G < Threshold && c.B < Threshold)
Jon Skeet
Thanks for answering! But i try it in another method by tolerance 50! but it does not work!!!
Jalal Amini
I also disable anti-aliasing!
Jalal Amini
Thank you! It works! g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
Jalal Amini