Domanda

Ok so I'm trying to get a little more advanced here and one of the things i want to learn how to do is get pixel color or data from a certain position on the screen. I have searched a lot but it seems most people want to do this in c or c++.

Im making a program that scans a location on the screen for a certain color. If that location contains anything with orange then the number in my window turns into a 1 for true or 0 for false. The background of my window is transparent, if that matters at all.

I have only come across Graphics.CopyFromScreen() and bitmap.GetPixel();

Thanks

È stato utile?

Soluzione

To capture specific rectangle form the screen use the following code

    public Bitmap CaptureFromScreen(Rectangle rect)
    {
        Bitmap bmpScreenCapture = null;

        if (rect == Rectangle.Empty)//capture the whole screen
        {
            rect = Screen.PrimaryScreen.Bounds;
        }

        bmpScreenCapture = new Bitmap(rect.Width,rect.Height);

        Graphics p = Graphics.FromImage(bmpScreenCapture);


            p.CopyFromScreen(rect.X,
                     rect.Y,
                     0, 0,
                     rect.Size,
                     CopyPixelOperation.SourceCopy);


        p.Dispose();

        return bmpScreenCapture;
    }

To Get The color form a specific location use the function

    public Color GetColorFromScreen(Point p)
    {
        Rectangle rect = new Rectangle(p, new Size(2, 2));

        Bitmap map = CaptureFromScreen(rect);

        Color c = map.GetPixel(0, 0);

        map.Dispose();

        return c;
    }

Altri suggerimenti

Please see the following reference, I think this is what you need:

http://www.codeproject.com/Articles/24850/Geting-pixel-color-from-screen-shoot-image

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top