位图无法在其自身上找到像素!
场景:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑抗锯齿意味着“.”的像素不是完全黑色。为什么不改变你的条件来只选择“非常暗”的像素呢?
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?