Click here to Skip to main content
15,920,576 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have follwing code
C#
Color [] cl = new Color[20];



          cl[0] = myBitmap.GetPixel(array_X[0], array_Y[21]);
          cl[1] = myBitmap.GetPixel(array_X[1], array_Y[22]);
          cl[2] = myBitmap.GetPixel(array_X[2], array_Y[23]);
          cl[3] = myBitmap.GetPixel(array_X[3], array_Y[24]);

and i want to get average color of all these points like
Color cc = Average(cl[0],cl[1],cl[2],cl[3]);
for example if cl[0]= 255,255,255 and cl[1]= 0,0,0,; cl[2] = 255,255,255
then avearge should be 255,255,255

how i can do this please suggest some ways??
Posted

In addition to the correct answer by Kenneth who addressed only WPF:

In System.Drawing, use System.Drawing.Bitmap.LockBits:
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.lockbits.aspx[^].

—SA
 
Share this answer
 
Dont use GetPixel, you should use CopyPixel[^] instead, as GetPixel is veeeery slow, arrays are much nicer :)

You forgot to specify if this is WPF or WinForm.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Apr-13 0:41am    
Agree. I credited your advice, but only for WPF (or course, this is OP's fault, first of all).
I added my advice for System.Drawing, please see Solution 2.
—SA
irfanniazi 8-Apr-13 6:52am    
Dear i am OK with GETpIXEL.it is an winform actually now i want to get the color which is more occuring in these pixels i.e if a circle contains 10 points out of which 7 are white and 3 are black then i want a method which should take these points clor and result should be that color is white as majority points are of white color???
Kenneth Haugland 8-Apr-13 6:57am    
Shirley you cant call me dear. However, you should use the fastest method as you might might as well learn the best method.
irfanniazi 8-Apr-13 6:53am    
as we do average like avg(45,35,40) then avg will be 40 in the same way i want to know average of colors how to do this in c#??
Kenneth Haugland 8-Apr-13 6:56am    
Sounds like you should convert the pricture to grayscale first.

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