I want my program to analyse geometric figures drawn by user. Figures must be filled with color. I have already found algorithm that checks if points are inside figure.
I have problem with getting pixel color.

Bitmap rysunek;
Pen pisak = new Pen(Color.Black, 1);
rysunek = new Bitmap(pictureBox1.Width, pictureBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
pictureBox1.Image = rysunek;

That is the way I connect points drawn by user: Graphics.FromImage(rysunek).DrawLine(pisak, x[i], y[j]); and that's how I check if pixel is black:

if (rysunek.GetPixel(i, j) == Color.Black)
{
  MessageBox.Show("inside");
}

Messagebox with text "inside" never appears. When I added variable in which was kept color of pixel kolor = rysunek.GetPixel(j, i); then after reaching pixel that suppose to be black, value of kolor is: "{Name=ff000000, ARGB=(255, 0, 0, 0)}" What am I doing wrong? Is there other way to check pixel color?

Recommended Answers

All 5 Replies

Your code looks alright at first sight.
But System.Drawing.Imaging.PixelFormat.Format32bppArgb seems a bit tricky to me (colordept etc.) perhaps try System.Drawing.Imaging.PixelFormat. DontCare
Hope this helps.

Come to think of it. Why not simply use rysunek = new Bitmap(pictureBox1.Width, pictureBox1.Height); ?

Come to think of it. Why not simply use rysunek = new Bitmap(pictureBox1.Width, pictureBox1.Height); ?

I've tried that, but when program was checking pixel by pixel if any of them is black it turned out that all pixels have the same color which is not true. Value of variable kolor was "{Name=0, ARGB=(0, 0, 0, 0)}" in every pixel.

I've found way to partially solve my problem, when I change if (rysunek.GetPixel(i, j) == Color.Black) to if (rysunek.GetPixel(j, i) == Color.FromArgb(0, 0, 0) comparison gives proper result.

Now I can go on with my program, but it still bothers me why if (rysunek.GetPixel(i, j) == Color.Black) didn't worked...

If I can find something more on it I let you know.

It took me a while but I think I have found the solution!
Color.Black gives "{Name=Black, ARGB=(255, 0, 0, 0)}"
rysunek.GetPixel(i, j) when it's on a black pixel gives "{Name=ff000000, ARGB=(255, 0, 0, 0)}"
So I think the overloaded == operator for Color, compares the names of the colors, unless you explicitly tell it to compare argb values as you did in your second try.
I think I learned something here!

commented: My C# Expert, it's not reputation exchanging, but really I learn from him a lot :) +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.