Bad code, baaaaad – it hurts my eyes!

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:

  1. public static void SaveIconFile(this IEnumerable<Image> images, string iconFilename)
  2. {
  3.     var writer = new IconFileWriter();
  4.     writer.Images.AddRange(images);
  5.     writer.Save(iconFilename);
  6. }


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.

Update: I didn’t find the time to do this last night after all. I’ll see if I can get to it over the weekend instead. In the meantime, I’ve found other code samples that approach parts of the problem differently, so it is probably better that I spend a little more time on it, and make sure I handle things like colour palattes properly…


About Jerome

I am a senior C# developer in Johannesburg, South Africa. I am also a recovering addict, who spent nearly eight years using methamphetamine. I write on my recovery blog about my lessons learned and sometimes give advice to others who have made similar mistakes, often from my viewpoint as an atheist, and I also write some C# programming articles on my programming blog.
This entry was posted in Programming and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s