es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

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.

Imagen1

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?

Imagen2

Tags:  , , , ,

Answer

  1. Avatar for davy.ai

    Para obtener el resultado deseado, puedes modificar el código de la siguiente manera:

    Bitmap bmp = new Bitmap(pictureBox1.Image);
    Graphics g = Graphics.FromImage(bmp);
    String text = "TextTile";
    
    Font font = new Font(DefaultFont.Name, 40); //aumentar el tamaño de la fuente
    SizeF size = g.MeasureString(text, font);
    int textwidth = size.ToSize().Width;
    int textheight = size.ToSize().Height;
    
    int y_offset = (int)(textheight * Math.Sin(45 * Math.PI / 180.0)); //usar textheight en lugar de textwidth
    
    //el seno del ángulo puede devolver un valor cero o negativo,
    //no funcionará con esta fórmula
    if (y_offset >= 0)
    {
        for (int y = 0; y < bmp.Height; y += y_offset)
        {
            int x_offset = 0;
    
            //dibuja múltiples líneas de texto en la misma fila
            while (x_offset < bmp.Width)
            {
                //mover a esta posición
                g.TranslateTransform(x_offset, y);
    
                //dibujar texto rotado alrededor de su centro
                g.TranslateTransform(textwidth / 2, textheight / 2);
                g.RotateTransform(-45);
                g.TranslateTransform(-textwidth / 2, -textheight / 2);
                g.DrawString(text, font, Brushes.Yellow, 0, 0);
    
                //restablecer
                g.ResetTransform();
    
                x_offset += textwidth * 3; //utilizar un espacio mayor entre las líneas
            }
        }
    }
    
    pictureBox1.Image = bmp;
    

    En el código modificado, hemos aumentado el tamaño de la fuente a 40 y utilizado textheight en lugar de textwidth para calcular y_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 incrementa x_offset en textwidth * 3 y dibuja el texto en cada desplazamiento. También hemos modificado las llamadas a TranslateTransform 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.

Comments are closed.