Click here to Skip to main content
15,860,972 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

 
QuestionReading pixel data from image without loading Pin
zahra nessri30-Jan-10 0:45
zahra nessri30-Jan-10 0:45 
AnswerRe: Reading pixel data from image without loading Pin
Amarnath S1-Feb-10 1:13
professionalAmarnath S1-Feb-10 1:13 
GeneralRe: Reading pixel data from image without loading Pin
zahra nessri1-Feb-10 19:15
zahra nessri1-Feb-10 19:15 
GeneralRe: Reading pixel data from image without loading Pin
Amarnath S1-Feb-10 19:31
professionalAmarnath S1-Feb-10 19:31 
GeneralError in calculation of i1 causing memory access errors Pin
Member 424260913-Jan-10 11:53
Member 424260913-Jan-10 11:53 
GeneralRe: Error in calculation of i1 causing memory access errors Pin
Amarnath S1-Feb-10 1:14
professionalAmarnath S1-Feb-10 1:14 
Generalpng images Pin
ingpeterson24-Sep-09 7:40
ingpeterson24-Sep-09 7:40 
GeneralReading pixel data from Image Pin
sandeeprattu26-Jun-09 19:37
sandeeprattu26-Jun-09 19:37 
Hi
Please tell me how to read pixel data from an image so that i can use it as a raw data to display that image.



Thanks
Sandeep
GeneralRe: Reading pixel data from Image Pin
Amarnath S29-Jun-09 1:07
professionalAmarnath S29-Jun-09 1:07 
Questionwidth = height? Pin
Huisheng Chen29-Apr-09 16:34
Huisheng Chen29-Apr-09 16:34 
AnswerRe: width = height? Pin
Amarnath S29-Apr-09 19:31
professionalAmarnath S29-Apr-09 19:31 
GeneralRe: width = height? Pin
Trollslayer21-Sep-09 9:07
mentorTrollslayer21-Sep-09 9:07 
GeneralRe: width = height? Pin
Amarnath S21-Sep-09 19:05
professionalAmarnath S21-Sep-09 19:05 

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.