0

its possible create a loop gif. The code at the moment, add a label on gif but he repeat 30 times and them stopped...

string originalImgPath = @"..\35.gif";
System.Drawing.Image IMG = System.Drawing.Image.FromFile(originalImgPath);
FrameDimension dimension = new FrameDimension(IMG.FrameDimensionsList[0]);
int frameCount = IMG.GetFrameCount(dimension);
int Length = frameCount;
GifBitmapEncoder gEnc = new GifBitmapEncoder();

for (int s = 0; s < 30; s++)
{
    for (int i = 0; i < Length; i++)
    {
        IMG.SelectActiveFrame(dimension, i);
        var aFrame = new Bitmap(IMG);

        Graphics graphics = Graphics.FromImage(aFrame);
        graphics.FillRectangle(Brushes.White, new Rectangle((aFrame.Width / (6)), aFrame.Height-50, Convert.ToInt32(aFrame.Width/1.5), (aFrame.Height/4)));

        graphics.DrawString("\tWelcome\n Andrezila!!", new System.Drawing.Font("Arial", 14, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, Convert.ToInt32(aFrame.Width / 1.3), aFrame.Height - 50);

        var bmp = aFrame.GetHbitmap();
        var src = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                            bmp,
                            IntPtr.Zero,
                            System.Windows.Int32Rect.Empty,
                            BitmapSizeOptions.FromEmptyOptions());

        gEnc.Frames.Add(BitmapFrame.Create(src));
    }
}

string saveImgFile = @"..\tenor-304976776.gif";
using (FileStream fs2 = new FileStream(saveImgFile, FileMode.Create))
{
    gEnc.Save(fs2);
}
James Thorpe
  • 31,411
  • 5
  • 72
  • 93
Cost
  • 43
  • 3
  • It's a little unclear what your question actually is - on the face of it, the code will indeed repeat the gif 30 times, or is it not doing that, and you're asking why? – James Thorpe Jul 17 '23 at 12:40
  • @JamesThorpe the code repeat 30 times, but i would like do infinity like a normal gif – Cost Jul 17 '23 at 13:05
  • Welcome to Stack Overflow! To repeat the animation indefinitely you need to set the loop count of the [Netscape application extension](https://giflib.sourceforge.net/whatsinagif/animation_and_transparency.html#animation) to zero. Unfortunately it looks like `GifBitmapEncoder` doesn't allow fine enough control over the encoding process to let you do this. [This question](https://stackoverflow.com/q/46445009/16563198) is relevant although asking how to stop looping completely, and the answer contains further links which may help you find something suitable to use instead. – sbridewell Jul 17 '23 at 15:50

0 Answers0