2009-04-02 14 views
10

Necesito agregar texto a un archivo de imagen. Necesito leer un archivo de imagen (jpg, png, gif) y necesito agregarle un texto de línea.Agregar texto a un archivo de imagen

+0

Mirar dentro de él. El problema es que cuando abre el archivo, está bloqueado por lo que no puede guardar en el mismo archivo. Volveremos a ti ... –

Respuesta

0

Puede hacerlo utilizando el objeto Graphics en C#. Puede obtener un objeto Graphics a partir de la imagen (image.CreateGraphics() - o algo así, tal como lo recuerdo) y utilizar algunos de los métodos integrados para agregarle texto como: Graphycs.DrawString() u otros métodos relacionados.

20

Bueno en GDI + lees en el archivo usando una clase de imagen y luego usas la clase Graphics para agregarle texto. Algo así como:

Image image = Image.FromFile(@"c:\somepic.gif"); //or .jpg, etc... 
    Graphics graphics = Graphics.FromImage(image); 
    graphics.DrawString("Hello", this.Font, Brushes.Black, 0, 0); 

Si desea guardar el archivo a través de la antigua, el código tiene que cambiar un poco como el método Image.FromFile() bloquea el archivo hasta que esté dispuesta. Lo siguiente es lo que ocurrió:

FileStream fs = new FileStream(@"c:\somepic.gif", FileMode.Open, FileAccess.Read); 
    Image image = Image.FromStream(fs); 
    fs.Close(); 

    Bitmap b = new Bitmap(image); 
    Graphics graphics = Graphics.FromImage(b); 
    graphics.DrawString("Hello", this.Font, Brushes.Black, 0, 0); 

    b.Save(@"c:\somepic.gif", image.RawFormat); 

    image.Dispose(); 
    b.Dispose(); 

Me gustaría probar esta bastante bien aunque :)

+0

Solo jugando al defensor del diablo aquí: ¡obviamente necesitarás guardarlo después! –

+0

Esperemos que lo anterior ayude. –

+0

cómo rotar este texto y luego agregarlo a la imagen? –

1

Específicamente para gifs para tener un resultado gif, usted debe escribir en cada cuadro como el siguiente:

string originalImgPath = @"C:\test.gif"; 
Image IMG = Image.FromFile(originalImgPath); 
FrameDimension dimension = new FrameDimension(IMG.FrameDimensionsList[0]); 
int frameCount = IMG.GetFrameCount(dimension); 
int Length = frameCount; 
GifBitmapEncoder gEnc = new GifBitmapEncoder(); 
for (int i = 0; i < Length; i++) 
{ 
    // Get each frame 
    IMG.SelectActiveFrame(dimension, i); 
    var aFrame = new Bitmap(IMG); 

    // write one the selected frame 
    Graphics graphics = Graphics.FromImage(aFrame); 
    graphics.DrawString("Hello", new Font("Arial", 24, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, 50, 50); 
    var bmp = aFrame.GetHbitmap(); 
    var src = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
       bmp, 
       IntPtr.Zero, 
       Int32Rect.Empty, 
       BitmapSizeOptions.FromEmptyOptions()); 
    // merge frames 
    gEnc.Frames.Add(BitmapFrame.Create(src)); 
} 

string saveImgFile = @"C:\modified_test.gif" 
using (FileStream fs2 = new FileStream(saveImgFile, FileMode.Create)) 
{ 
    gEnc.Save(fs2); 
} 

Debería haber mencionado que obtuve tramas gif de this post.

+0

https://stackoverflow.com/a/48395131/485008 –