Search code examples
c#imagegif

In C#,maybe it's a bug for Image.SaveAdd, who can help me to solve it?


I'm trying to combine two different gif file into one file.

First, I learned a lot about the gif format. And I know the delay time value is set in Graphics Control Extension which is a block of gif file.

I saved the first gif and set the FrameDelay value, code as below:

    ImageCodecInfo codeInfo = GetEncoder(ImageFormat.Gif);
    System.Drawing.Imaging.Encoder saveEncoder = System.Drawing.Imaging.Encoder.SaveFlag;
    EncoderParameters parameters = new EncoderParameters(1);

    parameters.Param[0] = new EncoderParameter(saveEncoder, (long)EncoderValue.MultiFrame);
    PropertyItem PropertyTagFrameDelay = img1.GetPropertyItem(0x5100);
    PropertyTagFrameDelay.Value = new byte[] { 0x96, 0x00 };// this is the delay value 0x0096, means 1.5 second
    img1.SetPropertyItem(PropertyTagFrameDelay);

    PropertyItem LoopCount = img1.GetPropertyItem(0x5101);
    LoopCount.Value = new byte[] { 0x00, 0x00 };// this means the gif loops endlessly
    img1.SetPropertyItem(LoopCount);

    img1.Save(@"c:\ddd.gif", codeInfo, parameters);

Then I tried to add another image as second frame.

    parameters = new EncoderParameters(1);
    parameters.Param[0] = new EncoderParameter(saveEncoder, (long)EncoderValue.FrameDimensionTime);
    PropertyTagFrameDelay = img2.GetPropertyItem(0x5100);
    PropertyTagFrameDelay.Value = new byte[] { 0x96, 0x00 };// this is the delay value 0x0096, means 1.5 second
    img2.SetPropertyItem(PropertyTagFrameDelay);

Last, I should terminate this image.

parameters = new EncoderParameters(1);
  parameters.Param[0] = new EncoderParameter(saveEncoder, (long)EncoderValue.Flush);
  img1.SaveAdd(parameters);

And I found that the second frame's delay time is always 0.

I tried a lot of method, but i have no idea to make it as 0x96.

So what's wrong with it?


Solution

  • This simply isn't supported by any of the .NET image encoders. Neither by GDI+ nor by WIC, the underlying native codecs for the System.Drawing.Bitmap and System.Windows.Media.Imaging.PngBitmapEncoder classes.

    While that sounds like a very strange oversight, the most probably reason was that GIF was encumbered by a software patent. Unisys owned the rights to the LZW compression algorithm and started aggressively pursuing obtaining the license fees for it. Starting at the most obvious targets where most money can be had, Microsoft is forever on the top of the list. They were not modest either, a non-commercial or private web site that used GIFs on their web pages had to cough up five thousand dollars in 1999.

    This killed the image format. Ubiquitous before this, just about everybody stopped using them. Stunningly quickly too, this only took a few months. Happily coinciding with everybody completely having their fill of animated gifs btw, it was grossly overdone before. You may find some web pages from back-then on the wayback machine where everything in the corner of eye was moving. Not the only lucky coincidence, it was the core reason for the open source PNG format being developed. Thank our lucky stars :)

    The patent expired around 2004, depending on where you live, so you won't have to fear a letter from Unisys anymore.

    Long story short, you'll have to shop around for another library to add this feature to your program. It is well-covered by this existing SO question, no need to repeat it here.