AForge.NET

  :: AForge.NET Framework :: Articles :: Forums ::

How do I get a int array of my pixels ?

The forum is to discuss topics related to different areas of image processing and computer vision.

How do I get a int array of my pixels ?

Postby btb4198 » Wed Jun 27, 2018 10:04 pm

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
btb4198
 
Posts: 29
Joined: Fri May 18, 2018 2:31 am

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

Postby andrew.kirillov » Thu Jun 28, 2018 7:10 am

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.
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 3359
Joined: Fri Jan 23, 2009 9:12 am
Location: UK




Return to Image Processing and Computer Vision