How to GetPixel from Bitmap?

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2009
Posts: 3
Reputation: kaczmar86 is an unknown quantity at this point 
Solved Threads: 0
kaczmar86 kaczmar86 is offline Offline
Newbie Poster

How to GetPixel from Bitmap?

 
0
  #1
Jan 2nd, 2009
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.
  1. Bitmap rysunek;
  2. Pen pisak = new Pen(Color.Black, 1);
  3. rysunek = new Bitmap(pictureBox1.Width, pictureBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  4. 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:
  1. if (rysunek.GetPixel(i, j) == Color.Black)
  2. {
  3. MessageBox.Show("inside");
  4. }
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,927
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is online now Online
Posting Virtuoso

Re: How to GetPixel from Bitmap?

 
0
  #2
Jan 2nd, 2009
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,927
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is online now Online
Posting Virtuoso

Re: How to GetPixel from Bitmap?

 
0
  #3
Jan 3rd, 2009
Come to think of it. Why not simply use rysunek = new Bitmap(pictureBox1.Width, pictureBox1.Height); ?
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 3
Reputation: kaczmar86 is an unknown quantity at this point 
Solved Threads: 0
kaczmar86 kaczmar86 is offline Offline
Newbie Poster

Re: How to GetPixel from Bitmap?

 
0
  #4
Jan 3rd, 2009
Originally Posted by ddanbe View Post
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...
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,927
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is online now Online
Posting Virtuoso

Re: How to GetPixel from Bitmap?

 
0
  #5
Jan 3rd, 2009
If I can find something more on it I let you know.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,927
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is online now Online
Posting Virtuoso

Re: How to GetPixel from Bitmap?

 
1
  #6
Jan 3rd, 2009
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!
Last edited by ddanbe; Jan 3rd, 2009 at 8:35 am.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC