Escribir Texto En Una Imagen en c#


Tengo el siguiente problema. Quiero hacer algunos gráficos en imagen de mapa de bits como bond form

Puedo escribir un texto en la imagen
pero voy a escribir más texto en varias posiciones

Bitmap a = new Bitmap(@"path\picture.bmp");

using(Graphics g = Graphics.FromImage(a))
{
 g.DrawString(....); // requires font, brush etc
}

¿Cómo puedo escribir texto y guardarlo y escribir otro texto en la imagen guardada

Author: Mr Alemi, 2011-07-26

4 answers

Para dibujar varias cadenas, llame a graphics.DrawString varias veces. Puede especificar la ubicación de la cadena dibujada. En este ejemplo dibujaremos dos cadenas "Hola", "Palabra" ("Hola "en color azul por adelantado" Palabra " en color rojo):

string firstText = "Hello";
string secondText = "World";

PointF firstLocation = new PointF(10f, 10f);
PointF secondLocation = new PointF(10f, 50f);

string imageFilePath = @"path\picture.bmp"
Bitmap bitmap = (Bitmap)Image.FromFile(imageFilePath);//load the image file

using(Graphics graphics = Graphics.FromImage(bitmap))
{
    using (Font arialFont =  new Font("Arial", 10))
    {
        graphics.DrawString(firstText, arialFont, Brushes.Blue, firstLocation);
        graphics.DrawString(secondText, arialFont, Brushes.Red, secondLocation);
    }
}

bitmap.Save(imageFilePath);//save the image file

Editar: "Agrego un código de carga y guardado".

Puede abrir el archivo de mapa de bits en cualquier momento Image.FromFile, y dibujar un nuevo texto en él utilizando el código anterior. y luego guarde el archivo de imagen bitmap.Save

 59
Author: Jalal Said,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-14 17:22:07

He Aquí un ejemplo de una llamada a Graphics.DrawString, tomado de aquí:

g.DrawString("My\nText", new Font("Tahoma", 40), Brushes.White, new PointF(0, 0));

Obviamente vuelve a tener instalada una fuente llamada Tahoma.

La clase Brushes tiene muchos pinceles incorporados.

Vea también, la página de MSDN para Graphics.DrawString.

 3
Author: George Duckett,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-07-26 07:57:12

Para guardar los cambios en el mismo archivo, tuve que combinar La respuesta de Jalal Said y la respuesta de NSGaga en esta pregunta. Necesita crear un nuevo objeto Bitmap basado en el antiguo, desechar el antiguo objeto Bitmap y luego guardarlo usando el nuevo objeto:

string firstText = "Hello";
string secondText = "World";

PointF firstLocation = new PointF(10f, 10f);
PointF secondLocation = new PointF(10f, 50f);

string imageFilePath = @"path\picture.bmp";

Bitmap newBitmap;
using (var bitmap = (Bitmap)Image.FromFile(imageFilePath))//load the image file
{
    using(Graphics graphics = Graphics.FromImage(bitmap))
    {
        using (Font arialFont =  new Font("Arial", 10))
        {
            graphics.DrawString(firstText, arialFont, Brushes.Blue, firstLocation);
            graphics.DrawString(secondText, arialFont, Brushes.Red, secondLocation);
        }
    }
    newBitmap = new Bitmap(bitmap);
}

newBitmap.Save(imageFilePath);//save the image file
newBitmap.Dispose();
 1
Author: Ibrahim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-26 14:48:43

Si Alguien tiene problemas con estas líneas de código:

using(Graphics graphics = Graphics.FromImage(bitmap))

La solución es :

Bitmap bitmap = (Bitmap)**System.Drawing.Image.FromFile**(@"C:\Documents and Settings\", true);
 -2
Author: sdasdasdasd,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-05 22:11:46