For the past few years, I've been searching for a .NET library that would load MNG (Multiple-image Network Graphics) animations and allow me to extract Bitmaps of the embedded images on a 'frame-by-frame' basis. Ideally, I was looking for something that was native .NET. Having failed to find a 100% native .NET solution, I decided to undertake a MNG library myself.
This article contains my initial work on such a library. It should be noted that the library contained within is NOT a 100% compatible MNG decoder per the MNG specification. My sole desire at this time was to extract embedded PNGs within a MNG. Therefore, no effort has been made to handle MNG Loops, Basis objects, Delta PNGs, Backgrounds, Frame definitions, or a whole host of other MNG 'chunks'.
Luckily, all the MNGs I deal with on a day-to-day basis are quite simple and this library fits my needs.
I won't go into much detail of what an MNG is or of the MNG file format. If you're interested in this article, then it's probably safe to say you're already somewhat knowledgeable in MNGs.
For much more detailed information, take a look at the following links:
Using the Code
The attached ZIP file contains a Visual Studio 2008 solution with two projects:
- MNG Viewer - WinForms application to demonstrate the library capabilities
SprinterMNG - The library responsible for reading MNGs
SprinterMNG contains one
MNG. It is through this class that MNGs are loaded.
Loading an MNG is as easy as:
MNG mng = new MNG();
mng.Load( filename );
Extracting the PNGs embedded in the MNG is as easy as:
int numEmbeddedPNG = mng.NumEmbeddedPNG;
for( int i = 0; i < numEmbeddedPNG; i++ )
Bitmap b = mng.ToBitmap(i);
I've attempted to structure the
SprinterMNG library such that the addition of MNG specification elements not covered in the initial version of the library would be as easy as possible.
Points of Interest
When I first decided to write this library, I was thinking that I would need to handle (parse, uncompress, render) the PNG image data myself. I got as far as implementing the decompression (using SharpZipLib) when I remembered that .NET natively supports loading of PNG file formats. Since each embedded PNG image within a MNG conforms to the PNG chunk specifications, I found that all I needed to do was pre-pend the PNG data extracted from the MNG with the PNG file signature to obtain a PNG formatted data stream. The resulting data stream (implemented as a
MemoryStream object in the library) can be used to create a Bitmap as shown below.
Bitmap b = (Bitmap)Bitmap.FromStream( pngs[index].ToStream() );
- April 9, 2009 - Initial revision of the library and sample application