C# 检查屏幕上是否出现图像
我有一个图像的裁剪版本,应该出现在我的屏幕上C# 检查屏幕上是否出现图像,c#,image,C#,Image,我有一个图像的裁剪版本,应该出现在我的屏幕上 Image 6Island=Image.FromFile(“C:\\Users\\6Island.png”) 现在,下一个目标是拍摄屏幕的图像 Bitmap CaptureScreen() { var image = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppA
Image 6Island=Image.FromFile(“C:\\Users\\6Island.png”)代码>
现在,下一个目标是拍摄屏幕的图像
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;
}
Image 6Island = Image.FromFile("C:\\Users\\6Island.png");
Image currentView = CaptureScreen();
然后我想看看是否可以在新图像中找到图像6Island
。而且颜色可能会略有不同。有什么办法吗?这只是一个快速、肮脏、非常缓慢的示例,但它可以工作。此代码对大位图进行“裁剪”,并将其与小位图进行比较。如果相等,则百分比必须为100,如果不相等,则百分比低于100。我会说,如果大于98%,那么你就找到了
private static void CompareBigAndSmallBitmaps(string fileName1, string fileName2)
{
var bmpBig = (Bitmap) Image.FromFile(fileName1);
var bmpSmall = (Bitmap) Image.FromFile(fileName2);
for (var offX = 0; offX < bmpBig.Width - bmpSmall.Width; offX++)
{
for (var offY = 0; offY < bmpBig.Height - bmpSmall.Height; offY++)
{
var percentage = CompareSmallBitmaps(bmpBig, bmpSmall, offX, offY);
if (percentage > 98.0) // define percentage of equality
{
// Aha... found something here....and exit here if you want
}
}
}
}
private static double CompareSmallBitmaps(Bitmap bmpBig, Bitmap bmpSmall, int offX, int offY)
{
var equals = 0;
for (var x = 0; x < bmpSmall.Width; x++)
{
for (var y = 0; y < bmpSmall.Height; y++)
{
var color1 = bmpBig.GetPixel(x + offX, y + offY).ToArgb();
var color2 = bmpSmall.GetPixel(x, y).ToArgb();
if (color1 == color2)
{
equals++;
}
}
}
return (Convert.ToDouble(equals)/Convert.ToDouble(bmpSmall.Width*bmpSmall.Height))*100.0;
}
private静态void比较位图(string fileName1,string fileName2)
{
var bmpBig=(位图)Image.FromFile(fileName1);
var bmpSmall=(位图)Image.FromFile(fileName2);
对于(变量offX=0;offX98.0)//定义相等百分比
{
//啊哈…在这里发现了一些东西…如果你想离开这里
}
}
}
}
专用静态双比较多位图(位图bmpBig、位图bmpSmall、int-offX、int-offY)
{
var等于0;
对于(变量x=0;x
逐像素比较pixel@x... 你建议我怎么做?解释一下“新图像中的6Island”是什么意思,你想在图像处理技术方面应用什么逻辑。我想看看某个图像是否出现在屏幕上。位图中的colorIn略有变化,有一个方法GetPixel,返回值为color。只需将第一个位图与第二个位图进行比较。从左上角到右下角运行。如果(color1==color2),这将不起作用,因为颜色可能会有点变化,如op中所述。需要一个带ε的颜色距离函数,或者可能还需要一个简单的截断:将每个通道向右移动1-4个像素,然后比较截断的颜色。。