Click here to Skip to main content
13,448,660 members (54,202 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 28 May 2007

PixelMap Class and PNM Image Viewer

, 28 May 2007
Rate this:
Please Sign up or sign in to vote.
A managed C# class and viewer for working with and converting Portable Bit Map (.pbm), Portable Grey Map (.pgm) and Portable Pixel Map (.ppm) images
Screenshot - PixelMapViewer.jpg


Although the class of Portable Any Map (PNM) format images -- which includes ASCII and Binary versions of Portable Bit Map (PBM), Portable Grey Map (PGM) and Portable Pixel Map (PPM) images -- represents the "lowest common denominator" of image formats, they are relatively uncommon in modern Windows environments. They also tend to be poorly supported on the free graphics programs for Windows, such as Paint and Paint.NET. Furthermore, I was not (in an admittedly short search) able to find a suitable C# class library for working with these formats programmatically.

Fortunately, the formats are quite simple, so it was relatively easy to build a PixelMap class to encapsulate these images and expose a couple of System.Drawing.Bitmap properties (BitMap and GreyMap) that can be used easily by other classes. Also, the simple Windows application that I created to test the PixelMap class rapidly evolved into a fairly competent viewer and converter that can save the current image as a BMP, JPG, TIFF, GIF, or PNG file.

Using the code

To use the PixelMap class you should:

  1. Copy the PixelMap.cs file into your own project.
  2. Change the namespace in the PixelMap.cs file to the name of your project namespace.
  3. Compile. The PixelMap class should now be available in your project.
The PixelMapViewer application demonstrates the usage of the PixelMap class. It also provides a handy viewing and converting application in its own right.

Points of interest

  • There are ASCII and Binary versions of each of the PBM, PGM, and PPM formats. The binary versions are much, much smaller and faster than the ASCII versions. They ought to be used unless there is a compelling reason to make the image file "human readable." Personally, I prefer to "read" my images by "looking at the pictures." This PixelMap class does not support the binary PBM format (Magic Number P4) at this time.
  • Since PBM, PGM, and PPM are UNIX-centric formats, their pixel order is BGR, rather than the RGB order that Windows programmers would tend to expect.
  • The "stride" of the image (normally calculated as stride = Image.Width * BytesPerPixel) should be a multiple of 4. If this is not the case, then in many programs the output images will become corrupted as they are processed. However, this is not a problem with the PixelMap class since it has been designed to work correctly with "off-size" images. However, it does have to use much less efficient (i.e. slow) algorithms relative to the normal direct-memory algorithms that it uses for well-sized images.


25 May, 2007 - Version uploaded to CodeProject


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


About the Author

Stephen Bogner
Engineer Defence R&D Canada
Canada Canada
Stephen Bogner is a Senior Research Engineer with Defence R&D Canada. As the Head Autonomous Applications Group, Autonomous Intelligent Systems Section, he only programs when it can't be avoided, and then only in C#.

You may also be interested in...

Comments and Discussions

QuestionBinary pbm P4 Format Pin
jazper11-Dec-13 13:28
memberjazper11-Dec-13 13:28 
QuestionSuggestion for Avoiding Stride Issues Pin
lewax0018-Sep-13 7:09
memberlewax0018-Sep-13 7:09 
GeneralMy vote of 5 Pin
manoj kumar choubey26-Feb-12 21:27
membermanoj kumar choubey26-Feb-12 21:27 
GeneralThank you! Pin
kcarrera6-Feb-11 9:48
memberkcarrera6-Feb-11 9:48 
Generalmemory leak patch Pin
leongurman6-Sep-10 20:12
memberleongurman6-Sep-10 20:12 
GeneralThanks for solving my problem Pin
jimmygyuma18-Mar-10 8:22
memberjimmygyuma18-Mar-10 8:22 
With just a few lines of code it converts a folder of ppm images to jpg images in another folder. My pdf image extraction program only seems to know ppm...
GeneralIt doesn't work for some *.pgm images. Pin
farid_colombia2-Oct-08 18:17
memberfarid_colombia2-Oct-08 18:17 
GeneralRe: It doesn't work for some *.pgm images. Pin
Stephen Bogner21-Oct-08 4:38
memberStephen Bogner21-Oct-08 4:38 
GeneralRe: It doesn't work for some *.pgm images. Pin
farid_colombia26-Oct-08 6:46
memberfarid_colombia26-Oct-08 6:46 
GeneralRe: It doesn't work for some *.pgm images. Pin
Mos Dan - Lucian12-Jan-10 8:30
memberMos Dan - Lucian12-Jan-10 8:30 
GeneralPLEASE HEPL ME !! Pin
xuanvuongspkt24-Sep-08 3:06
memberxuanvuongspkt24-Sep-08 3:06 
GeneralThanks! Pin
daothanhtuan25-Mar-08 10:29
memberdaothanhtuan25-Mar-08 10:29 
GeneralRe: Thanks! Pin
Stephen Bogner25-Mar-08 12:25
memberStephen Bogner25-Mar-08 12:25 
GeneralRe: Thanks! Pin
daothanhtuan3-Apr-08 9:18
memberdaothanhtuan3-Apr-08 9:18 
GeneralMuch appreciated Pin
SteveAbbott29-May-07 7:08
memberSteveAbbott29-May-07 7:08 
GeneralRe: Much appreciated Pin
Stephen Bogner1-Jun-07 7:48
memberStephen Bogner1-Jun-07 7:48 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04-2016 | 2.8.180318.3 | Last Updated 28 May 2007
Article Copyright 2007 by Stephen Bogner
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid