Click here to Skip to main content
15,860,943 members
Articles / Multimedia / GDI+
Article

Displaying 16-bit Images Using C#

Rate me:
Please Sign up or sign in to vote.
4.71/5 (12 votes)
8 Dec 2008CPOL3 min read 183.8K   8K   36   80
An article on how to use GDI+ methods for displaying 16-bit raw images.

Image 1

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)


Written By
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 short while. I love watching Kannada movies, and listening to Kannada songs. Currently studying and understanding the Bhagavad Geetha and teaching Sanskrit on YouTube.



Comments and Discussions

 
QuestionDisplaying 10-bit Images Pin
Member 1284456724-Mar-19 20:15
Member 1284456724-Mar-19 20:15 
QuestionSaving a 16 bit Tiff Image without compression Pin
NickIrtuganov24-Jan-16 9:10
NickIrtuganov24-Jan-16 9:10 
AnswerRe: Saving a 16 bit Tiff Image without compression Pin
Amarnath S24-Jan-16 20:34
professionalAmarnath S24-Jan-16 20:34 
GeneralRe: Saving a 16 bit Tiff Image without compression Pin
NickIrtuganov24-Jan-16 20:57
NickIrtuganov24-Jan-16 20:57 
GeneralRe: Saving a 16 bit Tiff Image without compression Pin
NickIrtuganov27-Jan-16 10:05
NickIrtuganov27-Jan-16 10:05 
GeneralRe: Saving a 16 bit Tiff Image without compression Pin
Amarnath S27-Jan-16 19:46
professionalAmarnath S27-Jan-16 19:46 
GeneralRe: Saving a 16 bit Tiff Image without compression Pin
NickIrtuganov31-Jan-16 3:13
NickIrtuganov31-Jan-16 3:13 
GeneralRe: Saving a 16 bit Tiff Image without compression Pin
Amarnath S31-Jan-16 7:32
professionalAmarnath S31-Jan-16 7:32 
GeneralRe: Saving a 16 bit Tiff Image without compression Pin
NickIrtuganov1-Feb-16 18:40
NickIrtuganov1-Feb-16 18:40 
GeneralRe: Saving a 16 bit Tiff Image without compression Pin
Amarnath S2-Feb-16 2:54
professionalAmarnath S2-Feb-16 2:54 
GeneralRe: Saving a 16 bit Tiff Image without compression Pin
NickIrtuganov2-Feb-16 8:34
NickIrtuganov2-Feb-16 8:34 
GeneralRe: Saving a 16 bit Tiff Image without compression Pin
Amarnath S2-Feb-16 13:57
professionalAmarnath S2-Feb-16 13:57 
GeneralRe: Saving a 16 bit Tiff Image without compression Pin
NickIrtuganov3-Feb-16 8:49
NickIrtuganov3-Feb-16 8:49 
GeneralRe: Saving a 16 bit Tiff Image without compression Pin
Amarnath S3-Feb-16 17:46
professionalAmarnath S3-Feb-16 17:46 
GeneralRe: Saving a 16 bit Tiff Image without compression Pin
NickIrtuganov3-Feb-16 20:49
NickIrtuganov3-Feb-16 20:49 
QuestionDisplaying a Tiff Image in time Pin
NickIrtuganov11-Jan-16 6:53
NickIrtuganov11-Jan-16 6:53 
AnswerRe: Displaying a Tiff Image in time Pin
Amarnath S11-Jan-16 21:23
professionalAmarnath S11-Jan-16 21:23 
QuestionHow to extend this program to display 16 bit TIFF file Pin
NickIrtuganov14-Nov-15 2:57
NickIrtuganov14-Nov-15 2:57 
AnswerRe: How to extend this program to display 16 bit TIFF file Pin
Amarnath S14-Nov-15 3:53
professionalAmarnath S14-Nov-15 3:53 
GeneralRe: How to extend this program to display 16 bit TIFF file Pin
NickIrtuganov14-Nov-15 4:41
NickIrtuganov14-Nov-15 4:41 
GeneralRe: How to extend this program to display 16 bit TIFF file Pin
NickIrtuganov14-Nov-15 8:31
NickIrtuganov14-Nov-15 8:31 
GeneralRe: How to extend this program to display 16 bit TIFF file Pin
Amarnath S14-Nov-15 15:52
professionalAmarnath S14-Nov-15 15:52 
GeneralRe: How to extend this program to display 16 bit TIFF file Pin
NickIrtuganov15-Nov-15 1:31
NickIrtuganov15-Nov-15 1:31 
GeneralRe: How to extend this program to display 16 bit TIFF file Pin
Amarnath S15-Nov-15 1:37
professionalAmarnath S15-Nov-15 1:37 
GeneralRe: How to extend this program to display 16 bit TIFF file Pin
NickIrtuganov15-Nov-15 3:25
NickIrtuganov15-Nov-15 3:25 

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.