Page 1 of 1

How do I get a int array of my pixels ?

PostPosted: Wed Jun 27, 2018 10:04 pm
by btb4198
How do I get a int array of my picture ?
here is my code :
Code: Select all
private void OpenBTN_Click(object sender, EventArgs e)
        {

           
            Bitmap img;
            int height = 0;
            int weight = 0;
            int size = 0;
            byte[] image;
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK) // Test result.
            {
                try
                {
                    img = AForge.Imaging.Image.FromFile(openFileDialog1.FileName);
                    ccdImageAnalyzer1.Image = img;
                     image = imageToByteArray(img);
                     height = img.Height;
                    weight = img.Width;
                    size = height * weight;
                   

                }
                catch( Exception err)
                {
                    MessageBox.Show(err.ToString());
                }
            }
            MessageBox.Show(result.ToString());
        }

public byte[] imageToByteArray(Bitmap imageIn)
        {
            MemoryStream ms = new MemoryStream();
            string temp = imageIn.PixelFormat.ToString();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            return ms.ToArray();
        }




I make a white picture in paint and saved it as a PNG
but when I run my program I am getting NON zero number in my array...
how is that ?
0 is white and 255 is black
but I am get random numbers
like

Code: Select all
137
80
78
71
13
10
26
10
0
0
0
13
73
72
68
82
0
0
3
185
0
0
3


how is that ?
something is wrong..
how do I get an true array of the pixels in my image ?

Also my array size is way off,
height 799 int
weight 953 int
size 761447 int
+ image {byte[605]} byte[]

so I know Height and weight are right because I make the pic in paint
so Size is right too
but the image array is way too small

Re: How do I get a int array of my pixels ?

PostPosted: Thu Jun 28, 2018 7:10 am
by andrew.kirillov
btb4198 wrote:but when I run my program I am getting NON zero number in my array...
how is that ?
0 is white and 255 is black

No, it is not. 0 is black, while 255 is white.

btb4198 wrote:but I am get random numbers

Did you know that PNG uses compression? If you read raw file, you will not get your pixel data. Even if it was uncompressed, there is still certain file structure, like header, for example.

Have a look at Bitmap.LockBits() - this is the way image processing is done in .NET.