How to search for an image on screen in C#?

Just a learner picture Just a learner · Feb 12, 2011 · Viewed 23.7k times · Source

I want to search for an image on screen using C# or other .NET languages(like powershell). Something like i give an image location in the file system and the code consider the whole screen as an image and search the image in the file system in the big image(the screen) then returns the image position on screen. I can't find this kind of things in the .net classes.

Thanks.

Answer

Markus Johnsson picture Markus Johnsson · Feb 12, 2011

This is a pretty specific problem, which is why you won't find it in the .NET Framework. You should break down your problem in smaller pieces:

Load image from file on disk

Use System.Drawing.Image.FromFile().

Acquire an image of the screen, i.e. a screen shot

Use System.Drawing.Graphics.CopyFromScreen():

Bitmap CaptureScreen()
{
    var image = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
    var gfx = Graphics.FromImage(image);
    gfx.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
    return image;
}

Find image inside image

See answer to this question.