Bitmap GetPixel() returning the incorrect value

S

ShakeDoctor

Hi, In my custom control's OnPaint method, I'm drawing a filled polygon to
an offscreen buffer like so:

Bitmap _bmp = new Bitmap(ClientSize.Width, ClientSize.Height);
Graphics gr = Graphics.FromImage(_bmp);
gr.Clear(Color.White);
SolidBrush brush = new SolidBrush(Color.FromArgb(100, 255, 255));
gr.FillPolygon(brush, poly.Points);

Then, in my OnMouseDown method, i'm trying to determine if the user clicked
on the polygon by checking the colour at the point where they clicked like
so:

protected override void OnMouseDown(MouseEventArgs e)
{
Color col = _bmp.GetPixel(e.X, e.Y);
if (col != Color.White)
{
if (col == Color.FromArgb(100, 255, 255))
{
// polygon clicked
}
}
}

However, _bmp.GetPixel() returns 248 when you click on an area of the bitmap
which isn't the polygon (instead of 255), and 96 when you click on the
polygon (instead of 100).

Why is this?? and how can I get round it to detect when the user has
clicked the polygon??

TIA.
 
I

Ilya Tumanov [MS]

All bitmaps in CF are screen bitmaps. Most screens on devices are 15 or 16
bits.
So, you're going to get 5 bits out of 8 for each color. 3 MSB will be set to
zero.
255 (1111 1111 ) will be changed to 248 (1111 1000) which is what you're
getting.
If you'd like to compare colors, mask out 3 LSBs before you do it.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Alex Feinman [MVP]

Actually on most PPC devices the color scheme used is 5:6:5 so you need to
strip bits intelligently. Of course this is all device dependent so he
really needs to put the color through a bitmap and see what comes out
 
S

ShakeDoctor

I suppose the right way to handle this would be to create a small (1x1)
bitmap, set a pixel, then get it and use that to compare colors.

Thanks for that - a great idea and it works a treat!
 

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