是否包含截图图像? [英] Contains screenshot image or not ?
问题描述
所以,我正在使用Windows Forms在C#中创建一个项目,我只有一个问题。我必须使用屏幕截图来显示图像。如果屏幕截图包含其中一个图像,则必须返回其路径(路径不重要)。所以我需要一个方法,如果截图包含Image,则返回true,否则返回false。在这种情况下,返回值并不重要。
我尝试过:
我试图使用以下代码,但它总是返回False,这意味着屏幕截图不包含这些图像。我使用以下代码:
So, I am making a Project in C# with Windows Forms and I have only one Problem. I have to campare images with Screenshot. If Screenshot contains one of those images it have to return it's Path(Path isn't Important). So I need a Method that can return true if Screenshot contains Image and returns false if it doesn't. In this case return Value isn't important.
What I have tried:
I have tried to use following Code but it is always returning False, it means that Screenshot contains none of those images. I am using following Code:
public static class imageCompare
{
public static string[] Compare(Bitmap bitmap)
{
string[] addressArray = Directory.GetFiles(Path.Combine(Directory.GetCurrentDirectory(), "Kartebi2"), "*.png");
string[] imageAddress = new string[addressArray.Length];
Bitmap[] bitmap1 = new Bitmap[36];
Bitmap[] bitmapImage = new Bitmap[36];
for (int i = 0; i < bitmap1.Length; i++)
{
bitmap1[i] = (Bitmap)System.Drawing.Image.FromFile(addressArray[i]);
bitmapImage[i] = Form1.ConvertToFormat(bitmap1[i], System.Drawing.Imaging.PixelFormat.Format24bppRgb);
}
for (int i = 0; i < bitmapImage.Length; i++)
{
if(Matching.Compare(bitmap, bitmapImage[i]))
imageAddress[i] = addressArray[i];
else
imageAddress[i] = null;
}
return imageAddress;
}
}
public static class Matching
{
public static bool Compare(this Bitmap template, Bitmap bmp)
{
const int divisor = 4;
const int epsilon = 10;
ExhaustiveTemplateMatching etm = new ExhaustiveTemplateMatching(0.9f);
TemplateMatch[] tm = etm.ProcessImage(new ResizeNearestNeighbor(template.Width / divisor, template.Height / divisor).Apply(template),
new ResizeNearestNeighbor(bmp.Width / divisor, bmp.Height / divisor).Apply(bmp));
if (tm.Length == 1)
{
Rectangle tempRect = tm[0].Rectangle;
if (Math.Abs(bmp.Width / divisor - tempRect.Width) < epsilon && Math.Abs(bmp.Height / divisor - tempRect.Height) < epsilon)
{
return true;
}
}
return false;
}
}
所以如果有人知道如何做这个比较请告诉我。
So if someone has other idea how to do this comparison tell me please.
推荐答案
请检查
在另一个图像中找到一个图像
[ ^ ]
找到包含在另一个位图内的位图 [ ^ ]
Please check
finding-an-image-inside-another-image
[^]
Finding a Bitmap contained inside another Bitmap[^]
这篇关于是否包含截图图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!