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

Displaying 16-bit Images Using C#

, 8 Dec 2008
Rate this:
Please Sign up or sign in to vote.
An article on how to use GDI+ methods for displaying 16-bit raw images.

Introduction and Background

Displaying images on the screen is quite easy with C#. You just need to read the image using the method Image.FromFile, and you have the image available for you. After this, you need to use the Graphics method DrawImage to display it. This works fine for different image formats like BMP, JPEG, PNG, TIFF, etc., and especially for color images, which form a large percentage of images commonly encountered. In color images, each pixel has values associated with the three channels - red, green, and blue.

However, for many scientific and specialized applications, these 'general purpose' color images are not suitable. For example, in X-ray applications, the images are grayscale. In grayscale images, the value of each pixel is a single value representing the intensity at a pixel location. It is sufficient to store only that single grayscale value. Grayscale images may have bit depths of 8, or 16, or higher. In an 8-bit image, the intensity value varies over 0 - 255, with each pixel value being stored in a byte; whereas in a 16-bit image, the intensity value varies over 0 - 65535, with each pixel value being stored in two contiguous bytes. Certain applications require the precision offered by a 16-bit image representation.

Raw Images

The simplest format for a grayscale image is the raw format, where the raw pixel data of the image from top left to bottom right is stored in the file. Since a raw file does not have any header, it is necessary to know a priori the width, height, and bit depth. Here, we present a simple application to display the image stored in raw format, with the following assumptions: the bit depth is 16 bits per pixel, and the width and height of the image are identical.

Displaying the Image

The steps in displaying the image are:

  1. Open the file and read in the pixel data. Use a BinaryReader object since this is a binary file. Store the pixel data in an ArrayList of ushort data type. From the file size, compute the width and height of the image.
  2. Create a Bitmap object of the required size, and scale the original pixel data from the range [0, 65535] to the range [0,255]. Do this because the display range is 0-255. For example, a pixel value of 30000 would become 117. Populate the pixels of the created Bitmap object. Here, there are two possibilities:
    1. using the SetPixel method, or
    2. using BitmapData and the LockBits and UnlockBits methods.

    The latter method requires the use of unsafe code, and is preferred since it is a lot faster than the former. The latter method is used in this application.

  3. Override the Paint method, and display the image using DrawImage.

In the sample application, a Panel object has been used to restrict the dimensions of the displayed image on the screen. Since it may be difficult to get 16-bit raw images, attached are a couple of 16-bit raw images of dimensions 512 x 512 pixels. This application has been built using Visual Studio 2003, and can easily be opened on later versions of Visual Studio. The application is to be built with the unsafe flag on.

Closure

This completes the small and simple application to display a 16-bit image. This application can be expanded to include reading of 8-bit and 16-bit TIFF files, to include scrollbars to navigate through the image, to use double-buffering to avoid flickering, and so on. Complex image processing applications can also be built using this basic structure as a starting point.

License

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

About the Author

Amarnath S
Architect
India India
Programming computers since about 1987, my first computer language was Fortran 77. Later I learnt C, C++ and C#. Also programmed a little in VB .Net. Worked with Enterprise Java for a while. Currently, interested in image processing. I love watching Kannada movies, and listening to Kannada songs.

Comments and Discussions

 
QuestionHow to display an image in the form TIFF (uncompressed) or as raw data using c # Pinmemberignasius fajar22-Feb-13 15:46 
AnswerRe: How to display an image in the form TIFF (uncompressed) or as raw data using c # [modified] PinmemberAmarnath S24-Feb-13 4:37 
GeneralRe: How to display an image in the form TIFF (uncompressed) or as raw data using c # Pinmemberignasius fajar24-Feb-13 15:29 
GeneralRe: How to display an image in the form TIFF (uncompressed) or as raw data using c # PinmemberAmarnath S24-Feb-13 16:24 
GeneralRe: How to display an image in the form TIFF (uncompressed) or as raw data using c # Pinmemberignasius fajar24-Feb-13 19:48 
GeneralRe: How to display an image in the form TIFF (uncompressed) or as raw data using c # PinmemberAmarnath S24-Feb-13 23:00 
GeneralRe: How to display an image in the form TIFF (uncompressed) or as raw data using c # Pinmemberignasius fajar26-Feb-13 16:26 
GeneralRe: How to display an image in the form TIFF (uncompressed) or as raw data using c # PinmemberAmarnath S26-Feb-13 16:35 
GeneralRe: How to display an image in the form TIFF (uncompressed) or as raw data using c # Pinmemberignasius fajar26-Feb-13 18:59 
GeneralRe: How to display an image in the form TIFF (uncompressed) or as raw data using c # PinmemberAmarnath S26-Feb-13 21:50 
QuestionArrayList to binaryfile Pinmembercooldj_0073-Nov-11 2:06 
AnswerRe: ArrayList to binaryfile PingroupAmarnath S3-Nov-11 21:47 
SuggestionLittle correction Pinmembermilansm7-Oct-11 8:07 
GeneralRe: Little correction PingroupAmarnath S14-Oct-11 22:01 
QuestionUnsafe required? PinmemberMember 803073426-Jul-11 5:16 
AnswerRe: Unsafe required? PingroupAmarnath S28-Jul-11 21:14 
GeneralRe: Unsafe required? Pinmembermilansm7-Oct-11 6:50 
GeneralDisplaying color images PinmemberMember 76584221-May-11 2:31 
GeneralRe: Displaying color images PingroupAmarnath S3-May-11 0:08 
GeneralDisplaying 8-bit image PinmemberMember 765842223-Apr-11 3:16 
GeneralRe: Displaying 8-bit image PingroupAmarnath S24-Apr-11 1:56 
Generalthe width and height of the image Pinmembercooldj_0078-Mar-11 18:52 
GeneralRe: the width and height of the image PingroupAmarnath S10-Mar-11 18:34 
QuestionReading pixel data from image without loading Pinmemberzahra nes30-Jan-10 0:45 
AnswerRe: Reading pixel data from image without loading PingroupAmarnath S1-Feb-10 1:13 

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 | Mobile
Web03 | 2.8.140721.1 | Last Updated 8 Dec 2008
Article Copyright 2008 by Amarnath S
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid