Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hi, please who can help me to show image end her level grays of and histogram with C#?
please I need your help
Posted
Updated 29-Apr-11 8:41am
v2
Comments
Sandeep Mewara 29-Apr-11 14:42pm    
Not clear.
Sergey Alexandrovich Kryukov 29-Apr-11 15:35pm    
Clear enough for me to answer, but incomplete tags forced me into double work -- please see.
--SA
ainosy 29-Apr-11 15:52pm    
i try to find code VS2008 to get method to convert an image to grayscale.and also show histogram
charles henington 29-Apr-11 15:18pm    
? what ? Please rephrase your question. grayscale maybe?
Sergey Alexandrovich Kryukov 29-Apr-11 15:35pm    
I answered, but incomplete tags forced me into double work -- please see.
--SA

First of all, you need a gray-scale bitmap. If it is colored, you need to work with if as with a gray scale, which would create some ambiguity: there can be several equally valid methods giving slightly different results.

You need to calculate color of all pixels and classify them into several groups of brightness. The number of desired groups is a free parameter; so you can have different results depending on it. I suggest you work with the arrahys of length of 2<sup>N</sup>, where N is some integer > 1. If you need to show full non-normalized histogram (which is preferred), you should create and array of N elements and define the brightness range for each array element with zero array element accepting pixels starting from zero (black) and last element — pixels of the brightest (maximum value, typically 255, but can be something like 2<sup>12</sup> to 2<sup>14</sup> (usual range for photo cameras raw mode these days; there are also so called HDR cameras).

How to get all pixels? It you use something like GetPixel, it would be infinitely long. Here is what wrong with you answer: you should have specified if you want to use System.Drawing or WPF (or something else) — the techniques are similar but completely different.

In brief, for System.Drawing use System.Drawing.Bitmap.LockBits, see the code sample: http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx[^]; for WPF, use System.Windows.Media.Imaging.BitmapSource and one of the methods CopyPixels, see http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapsource.aspx[^]; you will find some code samples in the help pages on CopyPixel.

—SA
 
Share this answer
 
v5
Comments
ainosy 29-Apr-11 15:49pm    
thinke u!
Sergey Alexandrovich Kryukov 29-Apr-11 15:52pm    
You're welcome.
Will you now formally accept this answer (green button)?
Thank you.
--SA
CPallini 29-Apr-11 15:51pm    
My 5++.
Sergey Alexandrovich Kryukov 29-Apr-11 15:54pm    
Thank you very much.
--SA
Kim Togo 29-Apr-11 16:09pm    
My 5++.
My solution was about converting a image to grayscale. OP is asking about making a histogram of a image. http://en.wikipedia.org/wiki/Image_histogram. Sorry :-)
As others have said, your question is not clear.

Assuming that (one of) the thing(s) you want is a histogram of the gray levels in your image, here are the things you'll need to do to get it.

I'm not going to write the code for you. I'll leave you to figure that out for yourself.

1. You need to get access to the array of pixels in the image. The .Net Bitmap object (perhaps the Image object, I don't remember) has methods that allow you to do this.

2. You need to scan the pixels across each line, for all lines, getting the byte(s) the comprise each pixel.

3. For each pixel, use the bytes to create a Color object (I assume that the image is not already a gray-scale image).

4. The .Net Color object has a GetBrightness() method. This returns a float value, which you'll want to quantize (since your histogram will have a fixed number of buckets in which to count pixels of a given brightness). Let's assume that your histogram has 256 buckets. Multiply the float by 255.99 and convert the result to an int - which will be in the range 0...255.

5. Use the int from step 4 as an index into an array of ints (which will be big enough unless you have an absolutely huge image) and add one to the value at the indexed position.

6. The array now has your histogram data, that you can use to draw lines (e.g. in a PictureBox) to visualize the histogram.
 
Share this answer
 
As a complement to solution from SA, and is not an answer.


http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale[^]

It seems to be a good article with more then 1 method to convert an image to grayscale.
I must say, I do not understand much about processing image/bitmap under C#, so it is a whole new world for me.
 
Share this answer
 
v3
Comments
ainosy 29-Apr-11 15:49pm    
u'r nice think u!
Kim Togo 29-Apr-11 16:18pm    
Thank you, although it does not answer your question. See answer from SA.
Sergey Alexandrovich Kryukov 29-Apr-11 15:54pm    
Kim, I want to compensate the vote of 1 with my 5. Even though it does not answer the question, it does not matter: you did not have to; I already answered the main part; and this is a nice complement, looks reasonable.
--SA
Kim Togo 29-Apr-11 16:12pm    
Thanks SA. It is late night here where I live, tired and will go to bed.
OP was asking about a histogram of a image. Not about converting, edit or manipulate.
I know this will get bad vote because already answered but wont be the first and wont be the last So here is a way to convert image to grayscale

C#
private Bitmap MakeGrayscale(Bitmap Bitmap)
       {
           //Declare myBitmap as a new Bitmap with the same Width & Height
           Bitmap myBitmap = new Bitmap(Bitmap.Width, Bitmap.Height);

           for (int x = 0; x < Bitmap.Width; x++)
           {
               for (int y = 0; y < Bitmap.Height; y++)
               {
                   //Get the Pixel
                   Color BitmapColor = Bitmap.GetPixel(x, y);

                   //Declare grayScale as the Grayscale Pixel
                   int grayScale = (int)((BitmapColor.R * 0.3) + (BitmapColor.G * 0.59) + (BitmapColor.B * 0.11));

                   //Declare myColor as a Grayscale Color
                   Color myColor = System.Drawing.Color.FromArgb(grayScale, grayScale, grayScale);

                   //Set the Grayscale Pixel
                   myBitmap.SetPixel(x, y, myColor);
               }
           }
           return myBitmap;
       }
 
Share this answer
 
Comments
charles henington 29-Apr-11 18:23pm    
Read SA's Post about converting Image To Grayscale and didn't pay attention to the post below my apoligies but here is a way to convert images to grayscale if needed. and Sa will be recieving my 5
ainosy 30-Apr-11 8:39am    
think u!!!
charles henington 2-May-11 20:36pm    
no problem hope it helps :)

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