picturebox detect pixel color

V

Vrijbuiter

I want to detect a pixel color number when i move the mouse over de
picturebox.
I think the next line has the color but i don't now how i get it:

System.Drawing.Point pt = new Point() ;
pt.X =151;
pt.Y =195;

this.textBox3.Text =
string.Format("{0}",pictureBox1.Image.Palette.Entries.ToString() ) ;
this line gives the string System.Drawing.Color[]

Can some one give me a hint?

With kind regards,
 
S

Stoitcho Goutsev \(100\)

You can't read the pixels color like that. The pallete is the colors used
inside the image, not the pixels itself. Further more the image my not have
color palette at all.
The result that you get from your code is correct. The ColorPalette.Entries
property is of type array of Colors - Color[]. There is nothing more that
the ToString can return, but the name of the type.

In order to read the pixels of an image you need to create a Bitmap from
that image and then use Bitmap's GetPixel method.

To create the Bitmap you can use the constructor overload that has an Image
parameter
 
T

Tom Spink

Vrijbuiter said:
I want to detect a pixel color number when i move the mouse over de
picturebox.
I think the next line has the color but i don't now how i get it:

System.Drawing.Point pt = new Point() ;
pt.X =151;
pt.Y =195;

this.textBox3.Text =
string.Format("{0}",pictureBox1.Image.Palette.Entries.ToString() ) ;
this line gives the string System.Drawing.Color[]

Can some one give me a hint?

With kind regards,

Hi Vrijbuiter,

Give this a bash:

///
Point pt = new Point();
pt.X = 151;
pt.Y = 195;

textBox3.Text = ((pictureBox1.Image as Bitmap).GetPixel(pt.X,
pt.Y) ).ToString();
///

There's absolutely zero error handling there, so if pictureBox1.Image
doesn't correctly cast to a bitmap, you're up debug creek, without a
breakpoint.
 
V

Vrijbuiter

Goutsev and Spinks thanks for the helpfull tips.
The function .GetPixel works very well thanks a lott.

a De Vrijbuiter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top