What do you mean it "returns the wrong colour"? Without your code it is difficult to tell what is going on, but this:
Bitmap red = (Bitmap) Bitmap.FromFile(@"F:\Temp\Red.bmp");
Bitmap green = (Bitmap) Bitmap.FromFile(@"F:\Temp\Green.bmp");
Bitmap blue = (Bitmap) Bitmap.FromFile(@"F:\Temp\Blue.bmp");
Color c = red.GetPixel(0, 0);
int argb = c.ToArgb();
Console.WriteLine("{0:X8}", argb);
c = green.GetPixel(0, 0);
argb = c.ToArgb();
Console.WriteLine("{0:X8}", argb);
c = blue.GetPixel(0, 0);
argb = c.ToArgb();
Console.WriteLine("{0:X8}", argb);
Produces:
FFFF0000
FF00FF00
FF0000FF
Which is what I would expect. If this is not what you are doing, please edit your question and provide a relevant code fragment.
UPDATE:
Bitmap myBitmap;
x=e.X;
y = e.Y;
Color color = myBitmap.GetPixel(x, y);
label1.Text="Color Of "+x.ToString()+" , "+y.ToString()+" is : "+color.Name.ToString()+" "+color.ToString();
Color color2 = Color.FromArgb(color.R, color.B, color.G);
panel1.BackColor = color2;
If that is your exact code, then I'm not surprised it doesn't work properly - in fact, it should give you errors.
Firstly: You don't appear to be assigning
myBitmap
to anything: which means that the
myBitmap.GetPixel
call should give you a Null Reference Exception.
Secondly: Why are you creating a
Color
from the Argb value of a
Color
(but ignoring the Alpha transparency)?
I assume you are using this code in a mouse click event handler or similar?
If so, then you need to get the graphics context for the control the mouse if over and get the pixel information from that - not a trivial job, but there is an example
here.[
^]