Click here to Skip to main content
15,885,537 members
Articles / Desktop Programming / Windows Forms

.NET MNG Viewer

Rate me:
Please Sign up or sign in to vote.
4.17/5 (5 votes)
9 Apr 2009CPOL2 min read 40.8K   566   13   10
A native .NET library and application to view the PNGs embedded in a MNG

Introduction

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.

Background 

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:

  1. MNG Viewer  - WinForms application to demonstrate the library capabilities
  2. SprinterMNG - The library responsible for reading MNGs

SprinterMNG contains one public class MNG.  It is through this class that MNGs are loaded.

Loading an MNG is as easy as:

C#
MNG mng = new MNG();
mng.Load( filename );  

Extracting the PNGs embedded in the MNG is as easy as:

C#
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. 

C#
Bitmap b = (Bitmap)Bitmap.FromStream( pngs[index].ToStream() ); 

History 

  • April 9, 2009 - Initial revision of the library and sample application

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Bally Technologies
United States United States
I've been software developer since early 1991. My focuses have ranged from deeply embedded (Intel and ARM architectures,) to firmware (C and C++), to application level software under Linux and Windows (C, C++ and C#.) Applications developed with .NET under Windows have been primarily test and support applications for other development activities.

Comments and Discussions

 
Generalmng creation Pin
jambonbill4-Nov-09 2:11
jambonbill4-Nov-09 2:11 
GeneralRe: mng creation Pin
SprinterDave8-Jan-10 6:01
SprinterDave8-Jan-10 6:01 
GeneralAPNG Viewer Pin
Huisheng Chen4-May-09 17:02
Huisheng Chen4-May-09 17:02 
GeneralRe: APNG Viewer Pin
SprinterDave5-May-09 1:49
SprinterDave5-May-09 1:49 
Questionhow about apng Pin
Huisheng Chen4-May-09 0:25
Huisheng Chen4-May-09 0:25 
AnswerRe: how about apng Pin
SprinterDave4-May-09 2:51
SprinterDave4-May-09 2:51 
GeneralRe: how about apng Pin
Huisheng Chen4-May-09 5:02
Huisheng Chen4-May-09 5:02 
GeneralRe: how about apng Pin
Huisheng Chen4-May-09 6:00
Huisheng Chen4-May-09 6:00 
GeneralRe: how about apng Pin
Huisheng Chen4-May-09 16:03
Huisheng Chen4-May-09 16:03 
Generaldelay Pin
Huisheng Chen9-Apr-09 23:15
Huisheng Chen9-Apr-09 23:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.