Dibujando texto diagonal sobre un mapa de bits.
Estoy utilizando el siguiente código para dibujar texto inclinado sobre una imagen. Esto funciona, pero no como se pretende.
Bitmap bmp = new Bitmap(pictureBox1.Image);
Graphics g = Graphics.FromImage(bmp);
String text = "TextTile";
Font font = new Font(DefaultFont.Name, 20);
SizeF size = g.MeasureString(text, font);
int textwidth = size.ToSize().Width;
int textheight = size.ToSize().Height;
int y_offset = (int)(textwidth * Math.Sin(45 * Math.PI / 180.0));
// El seno del ángulo puede devolver un valor cero o negativo,
// no funcionará con esta fórmula
if (y_offset >= 0)
{
for (int x = 0; x < bmp.Width; x += textwidth)
{
for (int y = 0; y < bmp.Height; y += y_offset)
{
// mover a esta posición
g.TranslateTransform(x, y);
// dibujar texto girado alrededor de su centro
g.TranslateTransform(textwidth, textheight);
g.RotateTransform(-45);
g.TranslateTransform(-textwidth, -textheight);
g.DrawString(text, font, Brushes.Yellow, 0, 0);
// restablecer
g.ResetTransform();
}
}
}
pictureBox1.Image = bmp;
Este es el resultado del código.
Lo que estoy buscando es algo como la imagen de abajo. Puedes ver que hay varias líneas de texto en la misma línea. ¿Cómo puedo ajustar el tamaño de fuente del texto para que la imagen tenga una marca de agua adecuada?
davy.ai
Para obtener el resultado deseado, puedes modificar el código de la siguiente manera:
En el código modificado, hemos aumentado el tamaño de la fuente a 40 y utilizado
textheight
en lugar detextwidth
para calculary_offset
. También hemos dibujado múltiples líneas de texto en la misma fila, con un espacio mayor entre las líneas. Para lograr esto, hemos agregado un bucle interno que incrementax_offset
entextwidth * 3
y dibuja el texto en cada desplazamiento. También hemos modificado las llamadas aTranslateTransform
para centrar el texto alrededor de su punto medio. Con estos cambios, la imagen resultante debería ser similar a la segunda imagen proporcionada en la pregunta.