Pixel Data to Byte Array

We are often asked how to retrieve the pixel data of a DicomImage into a Byte array. The following code will load an image and copy the pixel data into a byte array.

DicomImage img = new DicomImage(@"C:\...");
Array[] sysArray = (Array[])img.RawPixels;
Array FrameSysArray = sysArray[0];
byte[] bytesArray = new byte[FrameSysArray.Length * img.RawPixels.Length];
for (int frameCount = 0; frameCount < sysArray.Length; frameCount++)
{
 int offset = frameCount * FrameSysArray.Length;
 Buffer.BlockCopy(sysArray[frameCount], 0, bytesArray, offset, FrameSysArray.Length);        	 
}


If however you already know the type of data contained within the object Pixels and that is the type you require,then the code becomes simply a pass. (short array shown for 16bit images)

short[, ,] data = (ushort[, ,])dicomViewer1.CurrentImage.Pixels;
Relevance: