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 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?

8 Years
Discussion Span
Last Post by ddanbe

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...


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!

Votes + Comments
My C# Expert, it's not reputation exchanging, but really I learn from him a lot :)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.