Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
sorry my braziliam english.
im trying to do a little program that create a 2d array that returns 1 if have color or 0 if no but ever return 0. can anybody help me? i used a listbox to see the returns

C#
Bitmap newBitmap = new Bitmap(img.Width, img.Height);
 
byte[,] buffer = new byte[100,100]; 
 
for(int y=0;y<img.Height;y++) 
{ 
  for(int x=0;x<img.width;x++)>
  { 
   Color c = newBitmap.GetPixel(x,y); 
   int lumi = Convert.ToByte((int)(c.R*0.3 + c.G*0.59 + c.B*0.11));
   buffer[x,y] = Convert.ToByte((lumi  > 127) ? 1 : 0);
   
   listBox1.Items.Add(lumi);
  } 
} 
Posted

1 solution

The Bitmap[^] you are creating with
C#
Bitmap newBitmap = new Bitmap(img.Width, img.Height);
has a default black background. So all the values in your 2D array will be 0.

Before you start checking for colour you will need to place a colour picture in your bitmap.

Notes:
1 - Remove Convert.ToByte[^] from
C#
int lumi = Convert.ToByte((int)(c.R*0.3 + c.G*0.59 + c.B*0.11));
it serves no purpose.

2 - Your program will crash if either img.Width or img.Height is larger then 100.
 
Share this answer
 
Comments
difemen 20-Oct-11 7:54am    
if i remove the program cant compile. Cannot implicitly convert type 'int' to 'byte'.
BobJanova 20-Oct-11 8:47am    
int lumi = (int)(c.R*0.3 + c.G*0.59 + c.B*0.11));
buffer[x,y] = (lumi > 127) ? (byte)1 : (byte)0;

This is more efficient I think, because the byte constants shouldn't cause a runtime cast. You might want to consider

int lumi = c.R*30 + c.G*59 + c.B*11;
buffer[x,y] = (lumi > 12700) ? (byte)1 : (byte)0;

to avoid the coercion to and from floating point.
difemen 20-Oct-11 9:00am    
thanks a lot man. can you help me with the other problem?
difemen 20-Oct-11 7:56am    
yes i put with
void Button1Click(object sender, EventArgs e)
{
OpenFileDialog openFileDia = new OpenFileDialog();
if (openFileDia.ShowDialog() == DialogResult.OK)
{
this.img = Image.FromFile(openFileDia.FileName) as Bitmap;
this.setPicBoxImage();
}
}

private void setPicBoxImage()
{
this.pictureBox1.Image = img;
if (img.Height < pictureBox1.Height && img.Width < pictureBox1.Width)
{
this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;

}

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900