0

I have a .IMG files.
Specification/description of files is (I translated):
- The IMG binary format consists of N x N pixels, where the size of the individual pixel is 2 syllables of the numeric data type: short (predefined).

What I'm doing now is reading data into byte array and then trying to create BitImage with RGB values.

Byte array looks like this: [0] 0 byte
[1] 248 byte
[2] 0 byte
[3] 248 byte
[4] 0 byte
...
[414317] 253
[414318] 136
[414319] 254
[414320] 102
[414321] 252
[414322] 135
...
[524287] 248 byte

 public static Bitmap GetPictureFromData(int w, int h, byte[] data)
 {
     Bitmap bmp = new Bitmap(w, h);

     int i = 0;
     for (int y = 0; y < h; y++)
     {
         for (int x = 0; x < w; x++)
         {
             int a = 255;
             int r = data[i];
             int g = data[i+1];
             int b = data[i+2];

             bmp.SetPixel(x, y, Color.FromArgb(a,r, g, b));
             i += 3;
         }

     }
     return bmp;
}
private void button1_Click(object sender, EventArgs e)
{
     byte[] file = File.ReadAllBytes("C:\\Users\\user\\Desktop\\glava\\0219.img");

     Image img = GetPictureFromData(260, 260, file);
     pictureBox1.Image = img;
}

I don't know width and height of image, that's why I have put 260. And I used 260, because if I use greater number i goes out of bounds. I'm doing something wrong, because picture doesn't represent anything. I also used jpg format to test code, but also doesn't work.

7
  • 1
    Each pixel is represented by two shorts, so most probably 4 bytes - you are currently reading bytes in groups of three. Please give us more information on the input format. Commented Oct 13, 2017 at 9:50
  • if it's N x N, and each pixel is represented by ARGB, then (file.length / 4) ^ 2 should give you N, no? Commented Oct 13, 2017 at 9:50
  • Picture resolution is N x N, where N is Math.Sqrt(data.Length / 2) (short = 2 bytes). From 524288 bytes it's 512x512. And to read short from byte array you can either convert individual bytes (low byte + high byte * 256) or convert byte array to short array. Commented Oct 13, 2017 at 9:57
  • Oh okay, that helps for later, when I need to show image in dimension user defines (N). But now if I set dimension to 512X512 my i goes out of bounds. So I guess I'm not doing something right with RGB. @BerndFischer I can't give anything else, image is about CT scan and thats pretty much else. Commented Oct 13, 2017 at 10:19
  • I'm sorry: Without a better specification of the input format you're quite lost. If you have a lot of those images and/or know what they're supposed to look like, you may be able to find out more about the format. Commented Oct 13, 2017 at 19:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.