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:
- 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.
- 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:
- using the
SetPixel
method, or
- 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.
- 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.