(Color myColor = Color.LightBlue) returns value RGB{0x0}???

  • Thread starter Thread starter Sami
  • Start date Start date
S

Sami

Hello,
I just started out with Windows.Forms and was going through the MS
tutorials.
In the tutorial where you can create a non-rectangular shaped window by
using your own window painted with some paint utility, I am having some
problems with trying to compare the color of the pixel that I retrieve using
Bitmap.GetPixel(...) method.

My pseudo-code looks like this:
****************************************************************************
******
OnMouse_Click(...)
{
Color myColor = Bitmap.GetPixel(e.X, e.Y);
Color colorToCompare = Color.LightBlue; // At this point I notice
that the colorToCompare variable has a value of RGB{0x0}???!!! why?

if (myColor == colorToCompare)
// then execute logic which never does.
}
****************************************************************************
*******

I have another question. The reason I am trying to get the color of the
pixel is because I want to allow a mousemove event only when the user clicks
the mouse on the visible region of my form and not on its transparent
background. But I still have the problem that when a user clicks on the
transparent background of my window the icons (e.g.) that I see on that
position are not chosen? How can I choose the background items on a desktop
or if there is another application on the background when a user clicks on
the transparent background of my form?
 
Hi Sami,

There are two possible solutions for the colors not being equal.
1: myColor isn't LightBlue
2: myColor has a different alpha value than 255, which it probably might
if the image contains transparent parts.

To ignore the alpha value you can just check for

if(myColor.R == colorToCompare.R && myColor.G == colorToCompare.G &&
myColor.B == colorToCompare.B)
{
//the colors are sort of the same
}

As for the second question, I never had a problem with clicking on a
transparent part of my form not falling through. Not sure what kind of
transparency you are using, try setting the form's TransparencyKey
property to the same color as the form's backcolor. Use the same
backcolor for your image's Transparency (I never tried transparent forms
with bitmaps though, so I'm not sure they would work).
 
Hello,
Thanks for the prompt reply.
I might not have been very clear.

My first question was why I get a value of RGB{0x0} for Color.LightBlue (or
any other colour for that matter). Shouldn't it give me a valid RGB value?

As for the second question, I still don't understand your answer.
I am using a non-rectangular bitmap as the background for my form. Then I
set the Transparency Key Property of my form to the same colour as the
background colour of my bitmap image. By default I don't fall through. e.g.
if I have my form directly on top of the desktop I am able to see other app
icons through the transparent background of my form but am unable to start
the apps by double-clicking on them which makes sense since I didn't tell my
code to do that. How would I do that programmatically?
 
Another question that i have is whether there is any way to obtain the
PixelColor without using the Bitmap object?
e.g. How would I obtain the pixel color on a point on a form if I am not
using any bitmaps as the background image on a form.
 
Hi Sami,
Another question that i have is whether there is any way to obtain the
PixelColor without using the Bitmap object?
e.g. How would I obtain the pixel color on a point on a form if I am not
using any bitmaps as the background image on a form.

The "GetPixel" is a method of "Bitmap" class so you cannot use
it without "Bitmap" instance.
Some time ago i found a way to copy form (image) into the Bitmap,
but that solution needs "DllImport" of gdi32.dll.

HTH
Marcin
 
Back
Top