picture baox and counting the black pixels..

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi everyone, i am writing a program for my scool project and i need to count
the number of black pixels in a white empty picture box.. in the old visual
basic there was a really simple command called "Pset(x,y)" retruns the color
code of the specified location. but i couldnt found much things that does the
same thing in c sharp.. please tell me if u know anything abt this.. i tried
to use gdi but couldnt understand how does it works too much.. thanx for
spending you time on reading my question and for ur help :)
 
The image displayed in a PictureBox can be examined using the
Bitmap.GetPixel(x,y) method.

If the PictureBox is empty then it's a bit difficult to look at the contents
of the screen.

Given that you have a valid imagein the picturebox you can use something
like this to count the black pixels.

Bitmap myImage=(whatever image is in the picturebox)
int count=0;
for(int y=0; y<myimage.Height; y++)
{
for(int x=0; x<myimage.Width; x++)
{
Color c=myimage.GetPixel(x,y);
if(c.R==0 && c.G==0 && c.B==0)
count++;
}
}

The count now contains the number of black pixels.

Hope this helps.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Hi serdar c,

An empty PictureBox does not contain anything you can get colors off.
There is a GetPixel(x, y) you can use on the PictureBox's Bitmap, but
if the PictureBox is empty the Image property is null.

Bitmap b = (Bitmap)PictureBox.Image;
Color c = b.GetPixel(x, y);

On Mon, 20 Dec 2004 00:49:02 -0800, serdar c <serdar
 
thanks so much for helping, have a nice day :)

Bob Powell said:
The image displayed in a PictureBox can be examined using the
Bitmap.GetPixel(x,y) method.

If the PictureBox is empty then it's a bit difficult to look at the contents
of the screen.

Given that you have a valid imagein the picturebox you can use something
like this to count the black pixels.

Bitmap myImage=(whatever image is in the picturebox)
int count=0;
for(int y=0; y<myimage.Height; y++)
{
for(int x=0; x<myimage.Width; x++)
{
Color c=myimage.GetPixel(x,y);
if(c.R==0 && c.G==0 && c.B==0)
count++;
}
}

The count now contains the number of black pixels.

Hope this helps.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Hi,

Bitmap bmp = new Bitmap(pictureBox1.Image, pictureBox1.Size);
Color pixelColor = picture.GetPixel(x, y);

--Liam.
 
Back
Top