public void Load(Stream stream) { // Read the first 4 bytes from the stream to determine // what kind of image we are loading. byte[] magic = new byte [4]; stream.Read(magic, 0, 4); if (magic[0] == (byte) 'B' && magic[1] == (byte) 'M') { // Windows bitmap image. BmpReader.Load(stream, this); } else if (magic[0] == 0 && magic[1] == 0 && magic[2] == 1 && magic[3] == 0) { // Windows icon image. IconReader.Load(stream, this, false); } else if (magic[0] == 0 && magic[1] == 0 && magic[2] == 2 && magic[3] == 0) { // Windows cursor image (same as icon, with hotspots). IconReader.Load(stream, this, true); } else if (magic[0] == 137 && magic[1] == 80 && magic[2] == 78 && magic[3] == 71) { // PNG image. PngReader.Load(stream, this); } else if (magic[0] == (byte) 'G' && magic[1] == (byte) 'I' && magic[2] == (byte) 'F' && magic[3] == (byte) '8') { // GIF image. GifReader.Load(stream, this); } else if (magic[0] == (byte) 0xFF && magic[1] == (byte) 0xD8) { // JPEG or EXIF image. //JpegReader.Load(stream, this, magic, 4); // TODO: implement JPEG reading stream.Seek(0, SeekOrigin.Begin); JpegReaderHelper.Load(stream, this); } else { // Don't know how to load this kind of file. throw new FormatException(); } }