public byte[] GetBytes() { using (MemoryStream ms = new MemoryStream()) using (BinaryWriter br = new BinaryWriter(ms)) { if (HasPalette16) { br.Write((ushort)0x3045); throw new NotImplementedException(); } if (HasPalette256) { br.Write((ushort)0x304D); br.Write(GetColourPaletteBytes().ToArray()); } if (HasPicture256) { br.Write((ushort)0x3058); byte[] encoded = RLE.Encode(_picture256.ToByteArray()); encoded = LZW.Encode(encoded); br.Write((ushort)(encoded.Length + 5)); br.Write((ushort)_picture256.Width); br.Write((ushort)_picture256.Height); br.Write((byte)11); br.Write(encoded); } if (HasPalette16) { br.Write((ushort)0x3158); throw new NotImplementedException(); } return(ms.ToArray()); } }
public byte[] GetBytes() { using (MemoryStream output = new MemoryStream()) using (BinaryWriter writer = new BinaryWriter(output)) { // GIF header writer.Write("GIF89a".ToCharArray()); // Width x Height writer.Write((ushort)_pixels.Width); writer.Write((ushort)_pixels.Height); // GCT Descriptor writer.Write((byte)0xF7); // Background colour #0 writer.Write((byte)0x00); //Default pixel aspect ratio writer.Write((byte)0x00); // Write colour palette for (int i = 0; i < 256; i++) { byte r = 0, g = 0, b = 0; if (_palette.Length > i) { r = _palette[i].R; g = _palette[i].G; b = _palette[i].B; } writer.Write(new byte[] { r, g, b }); } // Image Descriptor writer.Write((byte)0x2C); // NW Corner writer.Write((ushort)0); writer.Write((ushort)0); // Width x Height writer.Write((ushort)_pixels.Width); writer.Write((ushort)_pixels.Height); // No local colour table writer.Write((byte)0x00); byte[] encoded = LZW.Encode(_pixels.ToByteArray(), true, false, 12); // Code length writer.Write((byte)0x08); foreach (byte[] byteBlock in ByteBlock(encoded)) { writer.Write((byte)byteBlock.Length); writer.Write(byteBlock); } writer.Write((byte)0x00); // End of file writer.Write((byte)0x3B); return(output.ToArray()); } }