The question already says everything, in the way I was doing, I wrote several characters, printava and put the program to count one by one: /
The question already says everything, in the way I was doing, I wrote several characters, printava and put the program to count one by one: /
Icarus, I developed the following solution based on a bitmap where text is written in black pixels and soon after a scan is performed on the same image counting the black and white pixels.
Follow the code:
//Instanciação do Bitmap (por padrão todos os pixels são brancos)
var bmp = new Bitmap(100, 100);
//Instanciação da fonte para impressão do(s) caracter(es) no Bitmap
var fonte = new Font(FontFamily.GenericSerif, 24);
//Um novo objeto do tipo Graphics é gerado a partir do Bitmap
var g = Graphics.FromImage(bmp);
//Logo após imprimimos o texto através do objeto Graphics
g.DrawString("X", fonte, new SolidBrush(Color.Black), 0, 0);
//Que por sua vez ao chamar Flush() grava os dados no Bitmap original.
g.Flush();
int count_black = 0, count_white = 0;
//Efetuamos a varredura pixel a pixel no Bitmap e verificamos quais pixels são brancos ou pretos e incrementamos o contador.
for (int x = 0; x < bmp.Width; ++x)
{
for (int y = 0; y < bmp.Height; ++y)
{
var pxl_color = bmp.GetPixel(x, y).ToArgb();
if (pxl_color == Color.Black.ToArgb()) ++count_black;
else ++count_white;
}
}
//Mostramos o resultado da varredura.
MessageBox.Show(count_black.ToString() + " pixels pretos e " + count_white.ToString() + " pixels brancos foram encontrados.");
The code was tested in a Windows Forms project.
The question does not say exactly everything. You mention the font of the letter, so I figured that your need involves knowing the dimensions of a text to do something about it, and not necessarily counting the pixels in the text. But I may be wrong. If your need is really to count the pixels, colleague @BrunoBermann's answer is the way (and also has that other question that may be useful). Otherwise, you need to use the font metrics (with the help of the Graphics.MeasureString
).
Here's an example (based on the example in the documentation) that calculates the text dimensions just to draw a red rectangle around it:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TesteSOPT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
// Cria a string a ser desenhada
String drawString = "Olá Mundo!";
// Cria a fonte e o brush de pintura
Font drawFont = new Font("Arial", 42);
SolidBrush drawBrush = new SolidBrush(Color.Black);
// Esse é o ponto a partir de onde o texto vai ser pintado (canto esquerdo-superior).
Point drawPoint = new Point(10, 20);
// Pinta o texto na janela (Form1).
e.Graphics.DrawString(drawString, drawFont, drawBrush, drawPoint);
// Desenha um retângulo vermelho ao redor do texto, usando as métricas de fonte
rectAround(drawPoint, drawFont, drawString, e.Graphics);
}
private void rectAround(Point drawPoint, Font drawFont, string drawString, Graphics g )
{
Size textSize = g.MeasureString(drawString, drawFont).ToSize();
Console.WriteLine(textSize);
Pen pen = new Pen(new SolidBrush(Color.Red), 5);
Rectangle rect = new Rectangle(drawPoint, textSize);
g.DrawRectangle(pen, rect);
}
}
}
This code produces the following output: