/// <summary> /// Write an image frame to the file /// </summary> /// <param name="image"><c>BitmapSource</c> to encode</param> public void AddFrame(BitmapSource image) { if (!Ready) { return; } if (!Animated && FrameCount == 1) { return; } if (image.PixelHeight != Height || image.PixelWidth != Width) { return; } if (image.Palette != ColorPalette) { return; } int nStride = (image.PixelWidth * image.Format.BitsPerPixel + 7) / 8; byte[] pixels = new byte[image.PixelHeight * nStride]; image.CopyPixels(pixels, nStride, 0); //Graphics Control Extension byte[] gct = new byte[8]; gct[0] = 0x21; gct[1] = 0xF9; gct[2] = 0x04; gct[3] = 0x00; //Packed Field gct.Replace(4, BitConverter.GetBytes((UInt16)FrameTime)); gct[6] = 0x00; //Transparency Color Index gct[7] = 0x00; //Image Descriptor byte[] id = new byte[10]; id[0] = 0x2C; id.Replace(1, BitConverter.GetBytes((UInt16)0)); //Left id.Replace(3, BitConverter.GetBytes((UInt16)0)); //Top id.Replace(5, BitConverter.GetBytes((UInt16)image.PixelWidth)); //Width id.Replace(7, BitConverter.GetBytes((UInt16)image.PixelHeight)); //Height id[9] = 0x00; //Packed Field DataStream.Write(gct, 0, gct.Length); DataStream.Write(id, 0, id.Length); //Image Data var lzw = new Gif.Components.LZWEncoder(image.PixelWidth, image.PixelHeight, pixels, image.Format.BitsPerPixel); lzw.Encode(DataStream); FrameCount++; }
private void ProcessQueue() { while (true) { if (ImageQueue.Count > 0) { Processing = true; var image = ImageQueue.Dequeue(); //Graphics Control Extension byte[] gct = new byte[8]; gct[0] = 0x21; gct[1] = 0xF9; gct[2] = 0x04; gct[3] = 0x00; //Packed Field gct.Replace(4, BitConverter.GetBytes((UInt16)FrameTime)); gct[6] = 0x00; //Transparency Color Index gct[7] = 0x00; //Image Descriptor byte[] id = new byte[10]; id[0] = 0x2C; id.Replace(1, BitConverter.GetBytes((UInt16)0)); //Left id.Replace(3, BitConverter.GetBytes((UInt16)0)); //Top id.Replace(5, BitConverter.GetBytes((UInt16)image.Width)); //Width id.Replace(7, BitConverter.GetBytes((UInt16)image.Height)); //Height id[9] = 0x00; //Packed Field DataStream.Write(gct, 0, gct.Length); DataStream.Write(id, 0, id.Length); //Image Data var lzw = new Gif.Components.LZWEncoder(image.Width, image.Height, image.PixelData, image.BitsPerPixel); lzw.Encode(DataStream); FrameCount++; Processing = false; } } }