C# 位图本身找不到像素!
情景: 1) 在位图上绘制字符串(通常为单个字符)的程序:C# 位图本身找不到像素!,c#,graphics,bitmap,pixel,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.S
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();
}
public Int16[]GetColoredPixcels(位图bmp,布尔通用)
{
列表像素=新列表();
int x=0,y=0;
做
{
颜色c=bmp.GetPixel(x,y);
如果(c.R==0&&c.G==0&&c.B==0)
添加((Int16)(x+(y+1)*bmp.Width));
如果(x==bmp.Width-1)
{
x=0;
y++;
}
其他的
x++;
}而(y
当输入字符是单点(.)时会出现问题。在处理函数bmp.GetPixel(x,y)时,我不知道位图对象中发生了什么,因为它找不到点位置!输出数组声明位图没有黑点!但当输入字符串为(:)时,程序可以正确地找到像素的位置
有什么建议或建议吗?
提前感谢…我怀疑抗锯齿意味着“.”的像素不是完全黑色的。为什么不改变你的条件,只选择“非常暗”的像素
private const int Threshold=10;
...
如果(c.R<阈值和c.G<阈值和c.B<阈值)
谢谢您的回答!但我用另一种方法尝试了50次!但是它不起作用!!!我也禁用反走样!非常感谢。它起作用了!g、 TextRenderingHint=System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
private const int Threshold = 10;
...
if (c.R < Threshold && c.G < Threshold && c.B < Threshold)