Click here to Skip to main content
15,905,867 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 186.6K   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

 
GeneralRe: How to extend this program to display 16 bit TIFF file Pin
NickIrtuganov30-Nov-15 18:14
NickIrtuganov30-Nov-15 18:14 
GeneralRe: How to extend this program to display 16 bit TIFF file Pin
Amarnath S1-Dec-15 17:36
professionalAmarnath S1-Dec-15 17:36 
GeneralRe: How to extend this program to display 16 bit TIFF file Pin
NickIrtuganov2-Dec-15 6:33
NickIrtuganov2-Dec-15 6:33 
GeneralRe: How to extend this program to display 16 bit TIFF file Pin
NickIrtuganov18-Dec-15 8:14
NickIrtuganov18-Dec-15 8:14 
GeneralRe: How to extend this program to display 16 bit TIFF file Pin
Amarnath S18-Dec-15 16:44
professionalAmarnath S18-Dec-15 16:44 
GeneralRe: How to extend this program to display 16 bit TIFF file Pin
NickIrtuganov19-Dec-15 6:45
NickIrtuganov19-Dec-15 6:45 
GeneralRe: How to extend this program to display 16 bit TIFF file Pin
NickIrtuganov23-Dec-15 12:21
NickIrtuganov23-Dec-15 12:21 
GeneralRe: How to extend this program to display 16 bit TIFF file Pin
Amarnath S23-Dec-15 16:53
professionalAmarnath S23-Dec-15 16:53 
Hi. I have added a button called 'Process' to just halve the pixel values. Here is the code. This works - reduces the intensity of the displayed image.

One word of caution with this code - it directly modifies the pixels16 values. In practice, you should make a copy of these values, so as to have the original values with you, in code.

Tell me if you find anything amiss.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

namespace ImageDraw
{
	/// <summary>
	/// Summary description for Form1.
	/// 
	/// It is assumed that the pixels comprising the image are stored 
	/// from left to right, and top to bottom, as unsigned short values.
	/// It is also assumed that the width and height of the image are 
	/// identical, so that the image dimensions can be computed by just 
	/// using the square root function.
	/// The algorithm is briefly as follows:
	///  1. Read the raw image file and store the pixel values into an 
	///     ArrayList called pixel16.
	///  2. Create a bitmap of the required dimensions. Create the bitmap 
	///     with the pixel format of Format24bppRgb, and set the red, green 
	///     and blue colors to be identical, since the images are grayscale. 
	///     Scale the 16-bit grayscale interval of 0 - 65535 to an 8-bit 
	///     interval of 0 - 255, since each color is represented by 8 bits. 
	///     Use the BitmapData class to populate the bitmap pixel values 
	///     from the abovementioned pixel16 array list. Use LockBits and 
	///     UnlockBits appropriately. Display the image. It is also 
	///     possible to use SetPixel to set the values, but this method 
	///     is painfully slow compared to what is used here.	
	///  3. Override Paint to draw the image.
	///  
	///  Compile with the unsafe flag on.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel panelImage;
		private System.Windows.Forms.Button bnOpenImage;
	
		private  List<ushort> pixels16;  // To hold the pixel data
		Graphics g;                   // Graphics of the panel object
		uint      width, height;      // Dimensions of the bitmap
		Bitmap   bmp;                 // Bitmap object

        TiffDecoder tiff;
        private Button bnProcess;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			pixels16 = new List<ushort>();
			g = panelImage.CreateGraphics();
            tiff = new TiffDecoder();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.bnOpenImage = new System.Windows.Forms.Button();
            this.panelImage = new System.Windows.Forms.Panel();
            this.bnProcess = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // bnOpenImage
            // 
            this.bnOpenImage.Location = new System.Drawing.Point(8, 8);
            this.bnOpenImage.Name = "bnOpenImage";
            this.bnOpenImage.Size = new System.Drawing.Size(88, 56);
            this.bnOpenImage.TabIndex = 0;
            this.bnOpenImage.Text = "Open TIFF Image";
            this.bnOpenImage.Click += new System.EventHandler(this.bnOpenImage_Click);
            // 
            // panelImage
            // 
            this.panelImage.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
            this.panelImage.Location = new System.Drawing.Point(104, 8);
            this.panelImage.Name = "panelImage";
            this.panelImage.Size = new System.Drawing.Size(512, 512);
            this.panelImage.TabIndex = 2;
            // 
            // bnProcess
            // 
            this.bnProcess.Location = new System.Drawing.Point(8, 70);
            this.bnProcess.Name = "bnProcess";
            this.bnProcess.Size = new System.Drawing.Size(88, 23);
            this.bnProcess.TabIndex = 3;
            this.bnProcess.Text = "Process";
            this.bnProcess.UseVisualStyleBackColor = true;
            this.bnProcess.Click += new System.EventHandler(this.bnProcess_Click);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(624, 530);
            this.Controls.Add(this.bnProcess);
            this.Controls.Add(this.panelImage);
            this.Controls.Add(this.bnOpenImage);
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Display 16-bit Raw Image Files";
            this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
            this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		// Open the Raw image file for reading.
		private void bnOpenImage_Click(object sender, System.EventArgs e)
		{
			pixels16.Clear();
			OpenFileDialog ofd = new OpenFileDialog();
			ofd.Filter = "TIFF Files(*.tif)|*.tif";
			ofd.ShowDialog();
			if( ofd.FileName.Length > 0 )
			{
                tiff.TiffFileName = ofd.FileName;
				//ReadImageFile(ofd.FileName);
                width = tiff.Width;
                height = tiff.Height;

                pixels16 = tiff.Pixels16;
				CreateBitmap();
				Invalidate();
			}
			ofd.Dispose();		
		}

		private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
			if( pixels16.Count > 0 )
				g.DrawImage(bmp, 0, 0);
		}

		private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			bmp.Dispose();
			pixels16.Clear();
		}	

		// Read the image file, and populate the array list pixels16.
		private void ReadImageFile(string fileName)
		{
			BinaryReader br = new BinaryReader(File.Open(fileName, FileMode.Open));
			ushort pixShort;
			int i;
			long iTotalSize = br.BaseStream.Length;

			for( i = 0; i < iTotalSize; i += 2 )
			{
				pixShort = br.ReadUInt16(); 
				pixels16.Add(pixShort);
			}
			br.Close();
			width = (uint)(Math.Sqrt(pixels16.Count));
			height = width;
		}

		// Create the Bitmap object and populate its pixel data with the stored pixel values.
		private void CreateBitmap()
		{
			bmp = new Bitmap((int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
			BitmapData bmd = bmp.LockBits(new Rectangle(0, 0, (int)width, (int)height),
				System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);

			// This 'unsafe' part of the code populates the bitmap bmp with data stored in pixel16.
			// It does so using pointers, and therefore the need for 'unsafe'. 
			unsafe
			{
				int    pixelSize = 3;
				int    i, j, j1, i1;
				byte   b;
				ushort sVal;
				double lPixval;

				for (i = 0; i < bmd.Height; ++i)
				{
					byte* row = (byte*)bmd.Scan0 + (i * bmd.Stride);
					i1 = i * bmd.Width;

					for (j = 0; j < bmd.Width; ++j)
					{
						sVal        = (pixels16[i1 + j]);
						lPixval     = (sVal / 256.0); // Convert to a 256 value range
						if( lPixval > 255 ) lPixval = 255;
						if( lPixval < 0   ) lPixval = 0;
						b           = (byte)(lPixval);
						j1          = j * pixelSize;
						row[j1]     = b;            // Red
						row[j1 + 1] = b;            // Green
						row[j1 + 2] = b;            // Blue
					}
				}
			}            
			bmp.UnlockBits(bmd);
		}

        // Half the pixel values
        private void bnProcess_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < pixels16.Count; ++i)
            {
                pixels16[i] /= 2;
            }
            CreateBitmap();
            Invalidate();

        }	
	}
}

GeneralRe: How to extend this program to display 16 bit TIFF file Pin
Amarnath S23-Dec-15 19:02
professionalAmarnath S23-Dec-15 19:02 
GeneralRe: How to extend this program to display 16 bit TIFF file Pin
NickIrtuganov23-Dec-15 23:31
NickIrtuganov23-Dec-15 23:31 
GeneralRe: How to extend this program to display 16 bit TIFF file Pin
Amarnath S24-Dec-15 21:19
professionalAmarnath S24-Dec-15 21:19 
GeneralRe: How to extend this program to display 16 bit TIFF file Pin
NickIrtuganov24-Dec-15 21:44
NickIrtuganov24-Dec-15 21:44 
GeneralRe: How to extend this program to display 16 bit TIFF file Pin
Amarnath S24-Dec-15 22:33
professionalAmarnath S24-Dec-15 22:33 
GeneralRe: How to extend this program to display 16 bit TIFF file Pin
NickIrtuganov11-Jan-16 6:50
NickIrtuganov11-Jan-16 6:50 
QuestionHow to display an image in the form TIFF (uncompressed) or as raw data using c # Pin
ignasius fajar22-Feb-13 15:46
ignasius fajar22-Feb-13 15:46 
AnswerRe: How to display an image in the form TIFF (uncompressed) or as raw data using c # Pin
Amarnath S24-Feb-13 4:37
professionalAmarnath S24-Feb-13 4:37 
GeneralRe: How to display an image in the form TIFF (uncompressed) or as raw data using c # Pin
ignasius fajar24-Feb-13 15:29
ignasius fajar24-Feb-13 15:29 
GeneralRe: How to display an image in the form TIFF (uncompressed) or as raw data using c # Pin
Amarnath S24-Feb-13 16:24
professionalAmarnath S24-Feb-13 16:24 
GeneralRe: How to display an image in the form TIFF (uncompressed) or as raw data using c # Pin
ignasius fajar24-Feb-13 19:48
ignasius fajar24-Feb-13 19:48 
GeneralRe: How to display an image in the form TIFF (uncompressed) or as raw data using c # Pin
Amarnath S24-Feb-13 23:00
professionalAmarnath S24-Feb-13 23:00 
GeneralRe: How to display an image in the form TIFF (uncompressed) or as raw data using c # Pin
ignasius fajar26-Feb-13 16:26
ignasius fajar26-Feb-13 16:26 
GeneralRe: How to display an image in the form TIFF (uncompressed) or as raw data using c # Pin
Amarnath S26-Feb-13 16:35
professionalAmarnath S26-Feb-13 16:35 
GeneralRe: How to display an image in the form TIFF (uncompressed) or as raw data using c # Pin
ignasius fajar26-Feb-13 18:59
ignasius fajar26-Feb-13 18:59 
GeneralRe: How to display an image in the form TIFF (uncompressed) or as raw data using c # Pin
Amarnath S26-Feb-13 21:50
professionalAmarnath S26-Feb-13 21:50 
QuestionArrayList to binaryfile Pin
cooldj_0073-Nov-11 2:06
cooldj_0073-Nov-11 2:06 

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.