Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the color of a specific pixel from a picture box?

Tags:

c#

picturebox

I have a picture box in one of my programs which displays my images just fine. What's displayed consists of a chosen "BackColor" and some filled rectangles using a brush and some lines using a pen. I have no imported images. I need to retrieve the color value of a specified pixel on the picture box. I've tried the following:

Bitmap b = new Bitmap(pictureBox1.Image);          
                Color colour = b.GetPixel(X,Y)

But pictureBox1.Image always returns null. Does .Image only work with imported images? If not how can I get this to work? Are there any alternatives?

like image 505
user3765211 Avatar asked Jun 22 '14 18:06

user3765211


People also ask

How do you make a pixel color?

With the get() function we can read the color of any pixel in our program window. We can specify which pixel we are interested in by using x and y coordinates as parameters. For example, color mycolor = get(100, 200); would grab the color of pixel 100, 200 and put that color into the mycolor variable.

How does each pixel get a color number?

A digital color image pixel is just numbers representing a RGB data value (Red, Green, Blue). Each pixel's color sample has three numerical RGB components (Red, Green, Blue) to represent the color of that tiny pixel area. These three RGB components are three 8-bit numbers for each pixel.

What colors are stored in a pixel?

Pixels are comprised of three colors: red, green, and blue, or RGB for short. The color you see from each pixel depends on the intensity of each of these three colors. The information stored about these colors is often represented by a set of three whole numbers between 0 and 255.

How do I change the bitmap color of a specific pixel in Android?

To set the colors of the pixels in your pixels array, get values from the static methods of Android's Color class and assign them into your array. When you're done, use setPixels to copy the pixels back to the bitmap.


1 Answers

Yes you can, but should you?

Here is the change your code needs:

Bitmap b = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.Height);
pictureBox1.DrawToBitmap(b, pictureBox1.ClientRectangle);
Color colour = b.GetPixel(X, Y);
b.Dispose();

But there is really no way around giving the PictureBox a real Image to work with somewhere if you want to do real work with it, meaning if you want to use its possibilities e.g. its SizeMode.

Simply drawing on its background is just not the same. Here is a minimal code to get a real Bitmap assigned:

public Form1()
{
   InitializeComponent();
   pictureBox1.Image = new Bitmap(pictureBox1.ClientSize.Width, 
                                  pictureBox1.ClientSize.Height); 
   using (Graphics graphics = Graphics.FromImage(pictureBox1.Image))
   { 
     graphics.FillRectangle(Brushes.CadetBlue, 0, 0, 99, 99);
     graphics.FillRectangle(Brushes.Beige, 66, 55, 66, 66);
     graphics.FillRectangle(Brushes.Orange, 33, 44, 55, 66);
   }  
}

However if you really don't want to assign an Image you can make the PictureBox draw itself onto a real Bitmap. Note that you must draw the Rectangles etc in the Paint event for this to work! (Actually you must use the Paint event for other reasons as well!)

Now you can test either way e.g. with a Label and your mouse:

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    if (pictureBox1.Image != null)
    {   // the 'real thing':
        Bitmap bmp = new Bitmap(pictureBox1.Image);
        Color colour = bmp.GetPixel(e.X, e.Y);
        label1.Text = colour.ToString();
        bmp.Dispose();
    }
    else
    {   // just the background:
        Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.Height);
        pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);
        Color colour = bmp.GetPixel(e.X, e.Y);
        label1.Text += "ARGB :" + colour.ToString();
        bmp.Dispose();
    }
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.FillRectangle(Brushes.DarkCyan, 0, 0, 99, 99);
    e.Graphics.FillRectangle(Brushes.DarkKhaki, 66, 55, 66, 66);
    e.Graphics.FillRectangle(Brushes.Wheat, 33, 44, 55, 66);
}
like image 153
TaW Avatar answered Sep 18 '22 18:09

TaW