2

I have an IMG file that I need to read and display as a picture. Each pixel is represented by 2 bytes type ushort.

So far I've read the file into a byte array and combined two bytes (byte0 and byte1, byte2 and byte3...) to create single ushort value, but now I'm lost on how to create an actual image from these values that seem to be ranging from zero to little over 65000.

IronCanTaco
  • 101
  • 1
  • 12
  • +1 for little over 65000 ;). ushort is 2^16 = 0 to 65,535. It all comes down to the format of your IMG file... for something standard each pixel would be represented by R(ed)G(reen)B(lue) value that varies from 0-255. Which would require each pixel to be 3 bytes (i.e. 3 x 2^8). So, if you can, post example image you are trying to parse - that would help for sure when it comes to figuring out the format. – nikib3ro Nov 03 '15 at 16:35
  • Oh, right, yes, 2^16 ... – IronCanTaco Nov 03 '15 at 16:40
  • As for the file, it's .img file of a ct scan, you can view the file here -> http://1drv.ms/1P7P130 (onedrive link) – IronCanTaco Nov 03 '15 at 16:46
  • @Matt nicely goes into detail in his answer down there... what I would try out of blue is to load your byte[] into MemoryStream and then use Image.FromStream(ms) - take a look at this question [Bytearray to image conversion](http://stackoverflow.com/questions/9173904/bytearray-to-image-conversion). If that fails then you can go into more detail as Matt suggests. – nikib3ro Nov 03 '15 at 16:50

1 Answers1

2

The classes you want to look at are Image and Bitmap.
Bitmap has a constructor that looks like this:

public Bitmap(
    int width,
    int height,
    int stride,
    PixelFormat format,
    IntPtr scan0
)

Its possible that you IMG is using the PixelFormat Format48bppRgb which is 16bits for Red, Green and Blue, so it might just work.

Failing that you could define the bitmap of the correct dimensions, then define the pixel format as above and then manually SetPixel() on every pixel in the image.

I haven't tried any of this, but hopefully it will be a steer in the right direction.

Code Gorilla
  • 962
  • 9
  • 23
  • Okay, I'm trying with setPixel but how do I go from ushort to color? – IronCanTaco Nov 03 '15 at 17:38
  • I see what you mean :( I would have though there would have been something to handle everything. However I did see this post and I think this might be what you are looking for. http://www.codeproject.com/Articles/482727/GDIplus-Deep-Color-Workaround – Code Gorilla Nov 04 '15 at 12:25