0

I am building an application that is able to get the pixels of pixels of a part of your screen. I am trying to do this by making the background of a form transparent, so that you can see through the form. The problem is, is that when I use the PanelToBitmap function I am getting the transparent color, instead of the pixels which are actually visible to the user.

Currently I'm using this.

Point point = new Point();

if (point.X > this.Location.X && point.X < this.Location.X + this.Width && point.Y > this.Location.Y + RectangleToScreen(this.ClientRectangle).Top - this.Top && point.Y < this.Location.Y + this.Height)
{
    point.X = point.X - this.Location.X;
    point.Y = point.Y - this.Location.Y;
    Bitmap img = (Bitmap)PanelToBitmap(this);
    Color color = img.GetPixel(point.X, point.Y);
    form.label1.Text = color.R.ToString();
    form.label2.Text = color.G.ToString();
    form.label3.Text = color.B.ToString();
}

Is there any function that is able to get the pixels that are actually visible to the user? I was thinking about the GetPixel function in the Gdi32 library, although people say it's slow.

Cœur
  • 34,719
  • 24
  • 185
  • 251
user2073973
  • 543
  • 5
  • 20

1 Answers1

1

I think you're looking for

Graphics.CopyFromSreen(..);

Create an Image, copy pixels from screen using Graphics.CopyFromSreen. Then you can use bitmap.GetPixel() to get the color at pixel

Refer Graphics.CopyFromSreen for more info

Sriram Sakthivel
  • 69,953
  • 7
  • 104
  • 182
  • It seems to be working, although for some reason this doesn't capture all windows. For example the color picker in paint.net doesn't get captured. Any ideas on why this could be? – user2073973 Sep 26 '13 at 12:21
  • Am afraid, It should work for all windows. May be it miss wpf elements? but not sure:) – Sriram Sakthivel Sep 26 '13 at 12:27
  • CopyFromScreen() has a bug. You'll need [this workaround](http://stackoverflow.com/a/3072580/17034). – Hans Passant Sep 26 '13 at 13:05