0

in my project i need add text on gif and i found a solution there, but the solution make the animation one time. I would like animated more times (like a real gif, dont stopped).

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);
}`
Andrezila
  • 25
  • 10
  • You want to save IMG, not fs2. You are adding text properly to the original IMG, but then not saving the results. You copied the wrong answer at your link and should be using Mladen Mihajlovic answer which has more votes. – jdweng May 29 '23 at 21:24
  • @jdweng the answer of Mladen dont work... i tried 2 times, with different gif file, but dont work. – Andrezila May 29 '23 at 21:32
  • The issue with your code is the save statement. It is saving wrong results. The save has to save the IMG object similar to Mladen solution instead of fs2. – jdweng May 29 '23 at 21:53
  • @jdweng i cannot understand. Can you show me a final code?? Thanks – Andrezila May 29 '23 at 23:43

0 Answers0