Adding text to an image file

前端 未结 3 1573
别那么骄傲
别那么骄傲 2021-01-04 12:57

I need to add text to an image file. I need to read one image file (jpg,png,gif) and I need add one line text to it.

3条回答
  •  有刺的猬
    2021-01-04 13:29

    Specifically for gifs to have a gif result, you should write on each frame like the following:

    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);
    }
    

    I should have mentioned that getting gif frames from this post.

提交回复
热议问题