HOWTO: create an animated GIF using .NET (C#)
By rickvdbosch
- 2 minutes read - 417 words.NET does not give you possibilities to create animated GIFs through GDI+. At least 1.1 doesn’t, they might incorporate it in 2.0. But there are ways to make them! This solution is one I used myself, and I’m very pleased!
//Variable declaration
StringCollection stringCollection;
MemoryStream memoryStream;
BinaryWriter binaryWriter;
Image image;
Byte[] buf1;
Byte[] buf2;
Byte[] buf3;
//Variable declaration
stringCollection = a_StringCollection_containing_images;
Response.ContentType = “Image/gif”;
memoryStream = new MemoryStream();
buf2 = new Byte[19];
buf3 = new Byte[8];
buf2[0] = 33; //extension introducer
buf2[1] = 255; //application extension
buf2[2] = 11; //size of block
buf2[3] = 78; //N
buf2[4] = 69; //E
buf2[5] = 84; //T
buf2[6] = 83; //S
buf2[7] = 67; //C
buf2[8] = 65; //A
buf2[9] = 80; //P
buf2[10] = 69; //E
buf2[11] = 50; //2
buf2[12] = 46; //.
buf2[13] = 48; //0
buf2[14] = 3; //Size of block
buf2[15] = 1; //
buf2[16] = 0; //
buf2[17] = 0; //
buf2[18] = 0; //Block terminator
buf3[0] = 33; //Extension introducer
buf3[1] = 249; //Graphic control extension
buf3[2] = 4; //Size of block
buf3[3] = 9; //Flags: reserved, disposal method, user input, transparent color
buf3[4] = 10; //Delay time low byte
buf3[5] = 3; //Delay time high byte
buf3[6] = 255; //Transparent color index
buf3[7] = 0; //Block terminator
binaryWriter = new BinaryWriter(Response.OutputStream);
for (int picCount = 0; picCount < stringCollection.Count; picCount++)
{
image = Bitmap.FromFile(stringCollection[picCount]);
image.Save(memoryStream, ImageFormat.Gif);
buf1 = memoryStream.ToArray();
if (picCount == 0)
{
//only write these the first time….
binaryWriter.Write(buf1, 0, 781); //Header & global color table
binaryWriter.Write(buf2, 0, 19); //Application extension
}
binaryWriter.Write(buf3, 0, 8); //Graphic extension
binaryWriter.Write(buf1, 789, buf1.Length – 790); //Image data
if (picCount == stringCollection.Count – 1)
{
//only write this one the last time….
binaryWriter.Write(“;”); //Image terminator
}
memoryStream.SetLength(0);
}
binaryWriter.Close();
Response.End();
Edit:
Damn you Paint.NET! I make a test-image: looks great. I save it as a GIF: still looks great. Use it in my anigif-code: no more greatness. I reopen the file in Paint.NET: then it’s fubar! Creating the testfiles in oldskool Paint solves this (because you only have GIF-supported colors in your toolbar)…
When you are converting a file to a GIF, old Paint warns you because you might be losing color information, Paint.NET does not. Paint reloads the file the way you saved it so you see what remains. Paint.NET does not. These are two wannahaves on Paint.NET for me!
Edit 2:
Thanks to Dennis for pointing out a small glitch in my type-work.
It should have been picCount == stringCollection.Count - 1
.