位图无法在其自身上找到像素!

发布于 2024-09-18 16:57:54 字数 1341 浏览 5 评论 0原文

场景:

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) 时位图对象发生了什么,因为它找不到点位置!输出数组声称位图没有黑点!但是当输入字符串是(:)时程序可以正确找到像素位置!

有什么建议或指导吗? 提前致谢...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

内心荒芜 2024-09-25 16:57:54

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

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文