3

In my application, there is an image uploading and if a user wants to upload gif image. I need to disable looping in an image and save it, in the result gif image of a user must be played once and stop after that. Maybe I need to delete pointer from the last frame of gif image? Any ideas?

Pavel Kononenko
  • 318
  • 3
  • 15
  • *"I need to disable looping in an image and save it"* - means you want to save a certain frame? – Sinatr Sep 27 '17 at 10:06
  • @Sinatr No, gif image it is part of advertisement banner on another site and one of the requirements from this site is gif without infinite looping. – Pavel Kononenko Sep 27 '17 at 10:10
  • @Sinatr do you mean, he should save a single frame as an another image and replace it on a client after loop is done? – ASpirin Sep 27 '17 at 10:12
  • Actually no, the gif image is an image which consists of frames, every frame has a pointer to next frame, so I need to remove the pointer from the last frame and save it. I need to modify gif and save it. IMHO – Pavel Kononenko Sep 27 '17 at 10:21
  • @PavelKononenko - What you need is removal of Netscape looping extension block. http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension – saurabheights Sep 29 '17 at 05:59

2 Answers2

0

You need to add or change the Netscape Application extension block. In this block there are 2 bytes to control the looping, see details in the following answer:

https://stackoverflow.com/a/28486261/3936440

I couldn't find a proper package for GIF manipulation but i think you can use BumpKit as an inspiration as mentioned here

https://stackoverflow.com/a/32810041/3936440

or

https://github.com/DataDink/Bumpkit/blob/master/BumpKit/BumpKit/GifEncoder.cs

ViRuSTriNiTy
  • 5,017
  • 2
  • 32
  • 58
0

I found an answer, thank you all who tried to helped me.

The gif image has a set of properties which you can get if you know the specific address of a byte of each property. For example, 20737 is looping property. if this property set to 0 gif image will have infinite looping, but if set this property great than 0 gif image will be repeated infinitely. The example I placed here.

var image = System.Drawing.Image.FromFile(path)
var IsLooped = BitConverter.ToInt16(image.GetPropertyItem(20737).Value, 0) != 1;
Pavel Kononenko
  • 318
  • 3
  • 15
  • You should also be aware of this :- https://bugzilla.mozilla.org/show_bug.cgi?id=1255497#c4 – saurabheights Sep 29 '17 at 06:00
  • Is this correct, isn't? Value 0 in "NETSCAPE2.0" means loop forever. Missing "NETSCAPE2.0" block means 0 repetitions = loop once. Value 1 in "NETSCAPE2.0" means 1 repetition = 2 loops. Value 2 in "NETSCAPE2.0" means 2 repetitions = 3 loops. – Pavel Kononenko Sep 29 '17 at 12:28
  • Hi Pavel, just checked an image. It is as mentioned in comment. Run this: `gifsicle --no-loopcount < input.gif > output.gif` and it will remove Netscape extension block and the image does not loops in both chrome & firefox. Confirmed using `xxd output.gif | grep NETSCAPE` and Netscape extension block was removed. Done on ubuntu. Hope it helps. Gifsicle reference - https://www.lcdf.org/gifsicle/man.html – saurabheights Sep 29 '17 at 16:10
  • 1
    By the way, I am not sure how to do in C#, but it would be better to check for the signature of NETSCAPE block and remove the 19 bytes from filestream and save back to stop gif from looping. – saurabheights Sep 29 '17 at 16:13