0

here is my code :

Image img;
int height = 0;
int weight = 0;
int size = 0;
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK) // Test result.
{
    try
    {
        img = Image.FromFile(openFileDialog1.FileName);
        ccdImageAnalyzer1.Image = img;
        byte[] image = imageToByteArray(img);
         height = img.Height;
        weight = img.Width;
        size = height * weight;
    }
    catch( Exception err)
    {
        MessageBox.Show(err.ToString());
    }
}
MessageBox.Show(result.ToString());

the problem is I am getting :

256 for height and 256 for width

and size is 65536

however my byte array image is size {byte[262198]} long.

how is this ?

pixels are from 0 - 255 and 255 is 1111 1111 and that is one byte, so the array should be only 65536 long...

so what is going on ?

public byte[] imageToByteArray(Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
    return ms.ToArray();
}

I am using a .Bmp picture

65536 * 4 = 262198

so it is 4 bytes for one pixels ?

Also, If that is true, then does pixel img(0,0) = image[0], image[1], image[2], image[3]?

if so,

should I convert it into a int ? so I can get the number 255 ?

I saved a white picture in paint as a ,bmp and I run my program and somehow I got non 0 values in my image array ?

how is that? I even saw some 255 but white is 0.

I just save a white pic.

the array should be fully of zero

maccettura
  • 10,514
  • 3
  • 28
  • 35
BBoone
  • 51
  • 8
  • 1
    I am pretty sure there is more than just the RGB per pixel information in a byte array. It stores all sorts of meta data too, encoding, alpha values, size, etc – maccettura Jun 27 '18 at 20:20
  • "*so it is 4 bytes for one pixels ?*" Obviously, as you already figured out, your image (at least its in-memory representation) uses 32bits per pixel (R,G,B, and alpha with 8 bits each). You can check the pixel format used by querying the `img.PixelFormat` property. "*Should I convert it into a int?*" Whether you could/should stick with a byte[] array or whether it would be benefical to rewrite your code to obtain an int[] array depends mostly on what you want to do with this array later in your program. –  Jun 27 '18 at 20:24
  • So I have a black picture that has a white circle on it. I am trying to find the radius of that circle in pixel – BBoone Jun 27 '18 at 20:39
  • maccettura, how do I delete or remove the meta data from the array? I just need the pixels – BBoone Jun 27 '18 at 20:41
  • @BBoone Currently you are saving the image as _image file_. Image file formats inevitably contain a file header... and on top of that, bmp will save your image upside down. If you only want the bare _image data bytes_ themselves, look into [LockBits and Marshal.Copy](https://stackoverflow.com/questions/49190596/c-sharp-rgb-values-from-bitmap-data/49859493#49859493). – Nyerguds Aug 01 '18 at 20:34

0 Answers0