Click here to Skip to main content
Click here to Skip to main content

Managed PNM Image, Reader and Writer

, 7 Jul 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
A class to read and write PNM files from System.Drawing.Image class
Screenshot - sample.jpg


This class supports reading the PNM image format into System.Drawing.Image and writing it back to PNM format. It makes it possible to inter convert different image file formats including PNM.


PNM is a portable bitmap format used in legacy software. The use of PNM file format is more common in LINUX / UNIX Platform. PNM file format consists of three different file formats namely PBM, PGM and PPM. (Corrections suggested by Andrew Kirillov, thank you.)

  • PBM [Portable Bit Map - Binary]
  • PGM [Portable Grey Map - GreyScale]
  • PPM [Portable Pixel Map - Color]

Each of these file formats, except PBM can be written in ASCII or Binary Encoding. The encoding is determined at reading time against the Identifier each PNM file contains called "Magic Number". This is always the first token of the PNM file.

  • "P1" [PBM - ASCII Encoding]
  • "P2" [PGM - ASCII Encoding]
  • "P3" [PPM - ASCII Encoding]
  • "P4" [PBM - Binary Encoding] (not yet implemented)
  • "P5" [PGM - Binary Encoding]
  • "P6" [PPM - Binary Encoding]

The next two tokens in the PNM header define width and height of the image. The fourth token is the maximum value of the pixel which is present only in the case of PGM and PPM. PBM does not contain this token. Moreover, the header can contain comments beginning with '#' character.

Read more about these image formats from the links provided in the Reference section.

16-bit Extensions

The original definitions of PGM and PNM do not describe 16-bit pixel formats. It has, however been used in many practical situations where accuracy/depth of color information matters. This library does not currently support the 16-bit extensions, perhaps an avenue for you to contribute. Read more about it here.

Using the Code

The parameterless constructor of System.Drawing.Image is marked internal and System.Drawing.Bitmap is marked as a sealed class, therefore I could not inherit them into my class and hence could not follow true object oriented design.

The code is primarily a class library, a class named PNM contains two overloaded functions to provide its functionality. Since the functions are static, you do not have to create an object of the said class.

Here is a sample use of the class:

// Read a PNM File into System.Drawing.Image
System.Drawing.Image im = ShaniSoft.Drawing.PNM.ReadPNM(FileName);

// Writing System.Drawing.Image into a PNM File
ShaniSoft.Drawing.PNM.WritePNM(im, FileName);

Both functions may throw IOException like a normal class would.

Points of Interest

PNM format is very plain in terms of complexity. It contains image subtype, width, height and max value of a pixel in ASCII format. The pixel data is then appended at the end in either binary format or ASCII format (nothing a BinaryReader/BinaryWriter couldn't handle).


  • Version 1, conversion from C to C# [PGM Only]
  • Version 2, complete PNM Support [PBM, PGM, PPM]
  • Corrections made to article regarding PNM format description



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


About the Author

Zeeshan Ejaz Bhatti
Web Developer
Pakistan Pakistan
BCSE - Software Engineering (2000 - 2004)
Foundation University Institute of Management and Computer Sciences.
MS - Computer Sciences (2004 - 2005)
Lahore Univeristy of Management Sciences

Comments and Discussions

GeneralSpeed increasing Pinmembersavamura2-Nov-08 12:35 
GeneralRe: Speed increasing PinmemberZeeshan Ejaz Bhatti4-Nov-08 5:54 
Generalpgm write in c++ Pinmemberwilliam cplus18-Jul-08 6:07 
AnswerRe: pgm write in c++ PinmemberZeeshan Ejaz Bhatti18-Jul-08 9:18 
GeneralIncorrect interepretation of formats PinmemberAndrew Kirillov7-Jul-08 8:21 
GeneralRe: Incorrect interepretation of formats PinmemberZeeshan Ejaz Bhatti7-Jul-08 19:49 
Generalwell done PinmemberAhmad hassanat30-Jun-07 14:24 
GeneralRe: well done Pinmembercowlinator19-Jan-10 19:16 
GeneralWhy not the whole PNM range? PinmemberWilli Deutschmann22-Mar-07 9:43 
GeneralRe: Why not the whole PNM range? Pinmemberdigitals200224-Mar-07 5:42 
GeneralConversion PinmemberBaselNimer19-Mar-07 6:59 
GeneralRe: Conversion Pinmemberdigitals200219-Mar-07 20:01 
GeneralRe: Conversion PinmemberBaselNimer20-Mar-07 6:26 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150224.1 | Last Updated 8 Jul 2008
Article Copyright 2007 by Zeeshan Ejaz Bhatti
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid