Click here to Skip to main content
16,017,922 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

First I say , excuse me about my language is not good.
And then, I have a problem in image processing in c#.
I get the argb, with GetPixel function.
But this return wrong color?

plz help me to this.


thanks

mohammad.r.khaliliazar
[DELETED]

[edit]Email deleted: Never post your email to any public forum, unless you really like spam! - OriginalGriff[/edit]



thanks for your help originalgriff
it's my code:

C#
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;
Posted
Updated 29-Jan-11 9:39am
v4

What do you mean it "returns the wrong colour"? Without your code it is difficult to tell what is going on, but this:

C#
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.[^]
 
Share this answer
 
v2
Comments
8611670474 29-Jan-11 15:43pm    
i want ,read a image from camera. it's ok
than , i want checked a color in this image.
for example ,if i have red color in this image ,
the program should say the image has red color.

-sorry , my english is not good , and i can't tell you better.
8611670474 29-Jan-11 15:52pm    
i chenged your code to this face:


Bitmap red = (Bitmap)Bitmap.FromFile(@"c:\Red.png");
Bitmap green = (Bitmap)Bitmap.FromFile(@"c:\Green.png");
Bitmap blue = (Bitmap)Bitmap.FromFile(@"c:\Blue.png");
Color c = red.GetPixel(0, 0);
int argb = c.ToArgb();
MessageBox.Show( argb.ToString());
c = green.GetPixel(0, 0);
argb = c.ToArgb();
MessageBox.Show(argb.ToString());
c = blue.GetPixel(0, 0);
argb = c.ToArgb();
MessageBox.Show(argb.ToString());


and this code return a nagativ integer
-1237980
-14503604
-16735512
OriginalGriff 29-Jan-11 15:58pm    
Yes it will: ARGB is 32 bit data (the same size as an int): the upper eight bits are the Alpha Transparency channel, then the Red info, then the green, then lastly the blue.
If your image is at all normal, the Alpha will probably be all ones, making your int negative.
See my original output example: the first two hex digits of each value are "FF" - all ones in the Alpha channel.
If you want to check for solid red, then use the Color directly, and test for
(Color.R == 255 && Color.G == 0 && Color.B == 0) instead. Be aware, it is unlikely to be there! You will probably need some "nearly there" code to give you an acceptable margin.
8611670474 29-Jan-11 16:01pm    
yes , it's in the mouse click and this is a test.
and i want compare to value of color in a pic.
for example.
i select green.
if my image has green in a area , the program say it to output.
8611670474 29-Jan-11 16:08pm    
hmm....i should translatr you comment. :D
anyway thank you for help , and it's very useful for me.
reaport my result :d
You say that you get the 'argb' from a call to GetPixel() but GetPixel returns a Color structure.

You also say that it returns the wrong color. What does this mean? How do you know it is the wrong color?

Without seeing your code, it is very difficult to answer your question.

Please edit your question (use the green 'Improve question' widget and add your code. Please don't forget to surround your code with <pre lang="cs">your code goes here</pre> tags.
 
Share this answer
 
Don't use GetPixel, it's slow. Read my image processing articles to see how to do it properly
 
Share this answer
 
Comments
8611670474 30-Jan-11 3:07am    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900