Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 482
Just a learner Avatar asked Feb 12 '11 12:02

Just a learner


1 Answers

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.

like image 124
Markus Johnsson Avatar answered Nov 02 '22 09:11

Markus Johnsson