There’s a downside to being obsessive, namely, you tend to obsess…
Today’s obsession relates to an animated Gif encoder for Windows Forms. Since my PanBox control (PictureBox replacement) used in the application that I have based most of my articles on supports displaying animated Gif files (it’s really easy – I use the ImageAnimator class code almost exactly from the help), I decided this morning that the application needs to support saving animated Gif files.
I’m working in Windows Forms, where there is no animated Gif encoder (Ja, there is one in WPF, but I won’t get there for a while yet). So I looked around and found an animated Gif encoder, which I downloaded. I’m sorry… excuse my language, but the code is so fucking bad, I can not stop myself from rewriting it. And that’s why I say being obsessive has its downside.
Amazingly, this is a very popular piece of code that’s been around for years. And OK, it works. All the guy did was take some Java code as is, and copy it into c#. Have you no pride? I don’t give a fuck that it works. That’s not the point. My code is not the greatest – far from it, but I do my best to follow accepted patterns and practices, and write code that I can be proud of.
My post on extracting images from PDF files using itextSharp is actually based on some Java code I found that related to the original library from Java, which fortunately turned out to be implemented along similar lines in itextSharp. I did a little more than copy and paste, and it was fun to exercise my brain a little, as we developers should do.
The Gif encoding code is quite trivial, but the structure of the code is what gets to me. It has two Start methods, one that takes an output file name and one that takes an output stream, then a SetDelay and SetRepeat method, an AddFrame method, and a Finish method. I suppose I’m overreacting, but I’d be ashamed to share such code.
I can’t imagine why I would ever want to pass in an output stream in the first place, and surely it’s common sense that properties like the Gif’s delay and repeat intervals should be properties.
Anyway, I will write an animated Gif encoder that makes more sense to me and is easier to use, and try to make it available for download tomorrow. I’m not sure what pattern the class will expose yet, but maybe something similar to my IconFileWriter.
And maybe I should add something similar to this, one of my “collection extension methods” that supports saving a collection of images to an icon file:
If the delay between all frames is identical, then surely only an AddFrame method is not enough. How about an AddFrames method, or AddRange? Of course there’s lots more that could be done, like adding a new derived Image type with support for multiframe images, like you can do with Tiff files. Anyway, tomorrow hopefully you can find some kind of animated Gif encoder implementation here, something that a bit of thought and effort went into creating.