Click here to Skip to main content
15,846,144 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Hello, I want to get only RGB values from an image present in picture box and show it on my form. Code I have tried gives the whole pixel values( and also takes too much time to calculate pixels ) but I need to show only RGB values. Please help. Thanks in advance.
I am working on Image Processing and using C#.Net, VS 2012. I am not using EMGU CV.

What I have tried:

public void PixelValues()
        {
            newBitmap = (Bitmap)pictureBox1.Image;
            Bitmap pix = (Bitmap)newBitmap.Clone();
            for (int i = 0; i < pix.Width; i++)
            {
                for (int j = 0; j < pix.Height; j++)
                {
                    Color pixel = newBitmap.GetPixel(i, j);
                    int a = pixel.A;//Alpha
                    int r = pixel.R;
                    int g = pixel.G;
                    int b = pixel.B;

                    listBox1.Items.Add("Red: " + r.ToString() + " - " + "Green: " + g.ToString() + " - " + "Blue: " + b.ToString());
                }
            }
        }
Posted
Updated 1-Nov-17 2:11am

You seem to have two issues 1) function, 2) speed.

1) Edit your question: Does your code already do what you want, only too slow?
If not, what did your code do differently than you wanted it to?

2 a) Wrap your textbox access code in
C#
listBox1.SuspendLayout();
listBox1.Items.AddAllTheInterestingStuff();
listBox1.ResumeLayout();
It saves a lot of time that is otherwise wasted on redrawing the listbox for every single entry.

2 b) Have a look at Fast Pointerless Image Processing in .NET. It shows a way of speeding up access to image data.
 
Share this answer
 
In addition to Solution 1, I would like to draw your attention on a few issues:

1. You don't need the pix variable, or at least not in the piece of code you have shown. Removing the copy could spare a few cycles (but that won't be a drastic improvement either).

2. You don't need the a variable, since you are not interested in the alpha value. Removing it will spare a fair amount of cycles since its assignation is done inside your nested for loops. r, g and b variables are not really mandatory either, unless for debuging purposes; but if you do not want to use them elsewhere than in the string building, you can get rid of them too.

3. String concatenations, especially in long loops, are also quite time consuming since they involve a lot of string copy operations internally. Here I would use a StringBuilder to try to gain some performance.
This way:
C#
using System.Text;

public void PixelValues()
{
   listBox1.SuspendLayout(); // Thanks to lukeer
   newBitmap = (Bitmap)pictureBox1.Image;
   Color pixel;
   StringBuilder sb = new StringBuilder(33); // 33 = max length of the string when all values are three digits long (quick count - could be wrong)
   for (int i = 0; i < pix.Width; i++)
   {
      for (int j = 0; j < pix.Height; j++)
      {
         pixel = newBitmap.GetPixel(i, j);
         sb.AppendFormat("Red: {0} - Green: {1} - Blue: {2}", pixel.R, pixel.G, pixel.B);
         listBox1.Items.Add(sb.ToString());
         sb.Clear();
      }
   }
   listBox1.ResumeLayout(); // Thanks to lukeer
}

You could also use a List<string> instead of adding items to the ListBox one by one, and add the whole lot of them at the end with the AddRange method of the ListBox class, but I'm not sure it would give a significant improvement. That could be tested, though, just to be sure.

Hope this helps. Kindly.
 
Share this answer
 
Comments
Member 13341679 25-Oct-17 3:02am    
@phil.o No changes in output, same as before.
phil.o 25-Oct-17 5:02am    
Just out ouf curiosity, what are the width and height of the picture?
Member 13341679 25-Oct-17 5:11am    
It's dynamic. One can take any kind of pictures. It's a UI.
phil.o 25-Oct-17 5:15am    
I get it. But what for the image you are using during your test?
Member 13341679 25-Oct-17 5:17am    
Image processing, applying different filters to enhance the picture clarity.
I found the solution and here is the code. It's giving the exact output I wanted.

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (opened)
                {
                    newBitmap = (Bitmap)pictureBox1.Image;

                    label1.Text = "X= " + e.X + " ; Y= " + e.Y;//Gives mouse pointer coordinate on the image
                    Color color = newBitmap.GetPixel(e.Location.X, e.Location.Y);
                    label2.Text = string.Format("RGB Values:  {0}, {1}, {2}", color.R, color.G, color.B);//Returns pixel value at mouse position   
                }
        }
 
Share this answer
 

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