2010-09-10 5 views
0

Scénario:Bitmap ne peut pas trouver un pixel sur lui-même!

1) Programme va dessiner une chaîne (généralement un seul caractère) sur un 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) en utilisant l'algorithme suivant, nous obtenons toutes les positions de pixels noirs:

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

Un problème survient lorsque le caractère d'entrée est un point unique (.). Je ne sais pas ce qui se passe dans l'objet bitmap lors du traitement de la fonction bmp.GetPixel(x, y), car il ne peut pas trouver la position du point! Le bitmap de revendications de tableau de sortie n'a aucun point noir! Mais quand la chaîne d'entrée est (:), le programme peut trouver la position des pixels correctement!

Une suggestion ou un guide? merci d'avance ...

Répondre

2

I suspect que l'anti-aliasing signifie que le pixel pour "." n'est pas complètement noir. Pourquoi ne pas changer votre condition pour simplement choisir des pixels "très sombres"?

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

Merci d'avoir répondu! Mais je l'essaie dans une autre méthode par la tolérance 50! mais ça ne marche pas !!! – Jalal

+0

Je désactive également l'anti-aliasing! – Jalal

+0

Merci! Ça marche! g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel; – Jalal

Questions connexes