C# 如何将鼠标放置在其他图像中找到的图像的一部分中?

C# 如何将鼠标放置在其他图像中找到的图像的一部分中?,c#,.net,vb.net,math,coordinates,C#,.net,Vb.net,Math,Coordinates,我正在使用AForge库在其他图像中查找图像(部分) (以下图片仅用于举例说明) 我正在使用1920x1080像素的桌面截图: 我在上面(55x557像素)搜索并定位了这张图片: 但我将两个图像的大小调整为25%(以获得比较速度),因此当我比较图像时,桌面屏幕截图是480x270px。切割后的图像为13x14px 使用AForge库,它会返回已调整大小的桌面屏幕内找到(剪切)图像的相对坐标shot,坐标为x=86,y=200 现在我需要在我的dektop中设置鼠标位置,在VMWare图标的中

我正在使用
AForge
库在其他图像中查找图像(部分)

(以下图片仅用于举例说明)

我正在使用1920x1080像素的桌面截图:

我在上面(55x557像素)搜索并定位了这张图片:

但我将两个图像的大小调整为25%(以获得比较速度),因此当我比较图像时,桌面屏幕截图是
480x270
px。切割后的图像为
13x14
px

使用
AForge
库,它会返回已调整大小的桌面屏幕内找到(剪切)图像的相对坐标shot,坐标为
x=86,y=200

现在我需要在我的dektop中设置鼠标位置,在VMWare图标的中心(更准确地说是在找到的剪切图像的中心),这里是我困惑的地方,在那里设置鼠标位置的算术运算是什么

我会记得:

决议:

我的桌面:1920x1080

图1:1920x1080

要在图像1中找到的图像:55x57

调整大小的图像1:480x270

要在Image1中找到的已调整大小的图像:13x14

在Image1中找到的已调整大小的图像的相对坐标:


x=86,y=200

如果你真的找到了位置,并且如果你真的在使用缩小到1/4的完整桌面,而不是窗口,那么你可以简单地乘回到原来的比例,然后像这样移动鼠标:

newPos= new Point(foundX * 4, foundY * 4);
Cursor.Position = newPos;
newPos= new Point(foundX * 4 + originalWidth / 2, foundY * 4 + originalHeight / 2);
如果您的FoundPosition不是中间位置,而是左上方位置,您可以像这样调整newPos:

newPos= new Point(foundX * 4, foundY * 4);
Cursor.Position = newPos;
newPos= new Point(foundX * 4 + originalWidth / 2, foundY * 4 + originalHeight / 2);

如果您在窗口中,在设置鼠标位置之前,还必须使用
PointToScreen()
功能计算相对位置到屏幕坐标。

缩小图像时,您可以执行以下操作:

intReducePct = 25
ReducedImg1 = ResizeImage(desktopBMP, intReducePct)

' save a restore factor
intFactor = (100 / intReducePct)      ' == 4

' I dont know what the AFOrge search returns, a Point probably
foundPt = Aforge.FindImgInImg(...)

' convert fountPt based on reduction factor (you reduced by 1/4th,
'    so scale up by 4x, basically)
Dim actualPoint As New Point(foundPt.X * intFactor, foundPt.Y * intFactor) 
a大返回x=86,y=200;86*4=344;200*4=800,但这是原始图像中的左上角(?),您显然想要找到的图像的中心,因此也调整原始位图:

' convert fountPt based on reduction factor + bmpFind size:
Dim actualPoint As New Point((foundPt.X * intFactor) + (bmpFind.Width \ 2),
                             (foundPt.Y * intFactor) + (bmpFind.Height \ 2))

bmpFind
将是还原前的原始图像。Option Strict将坚持使用某些CTypes,但这应该是它的要点。

我想与大家分享我为简化问题而编写的通用使用方法:

''' <summary>
''' Finds a part of an image inside other image and returns the top-left corner coordinates and it's similarity percent.
''' </summary>
''' <param name="BaseImage">
''' Indicates the base image.
''' </param>
''' <param name="ImageToFind">
''' Indicates the image to find in the base image.
''' </param>
''' <param name="Similarity">
''' Indicates the similarity percentage to compare the images.
''' A value of '100' means identical image. 
''' Note: High percentage values with big images could take several minutes to finish.
''' </param>
''' <returns>AForge.Imaging.TemplateMatch().</returns>
Private Function FindImage(ByVal BaseImage As Bitmap,
                           ByVal ImageToFind As Bitmap,
                           ByVal Similarity As Double) As AForge.Imaging.TemplateMatch()

    Dim SingleSimilarity As Single

    ' Translate the readable similarity percent value to Single value.
    Select Case Similarity

        Case Is < 0.1R, Is > 100.0R ' Value is out of range.
            Throw New Exception(String.Format("Similarity value of '{0}' is out of range, range is from '0.1' to '100.0'",
                                              CStr(Similarity)))

        Case Is = 100.0R ' Identical image comparission.
            SingleSimilarity = 1.0F

        Case Else ' Image comparission with specific similarity.
            SingleSimilarity = Convert.ToSingle(Similarity) / 100.0F

    End Select

    ' Set the similarity threshold to find all matching images with specified similarity.
    Dim tm As New AForge.Imaging.ExhaustiveTemplateMatching(SingleSimilarity)

    ' Return all the found matching images, 
    ' it contains the top-left corner coordinates of each one 
    ' and matchings are sortered by it's similarity percent.
    Return tm.ProcessImage(BaseImage, ImageToFind)

End Function

第一个问题-你确定它找到了吗?如果你在Photoshop中加载图像,它真的在那个位置吗(你有一个输入点:
x=86,x=200
2x?)。也就是说,越大的是越小的4倍,因此x=344y=800似乎是正确的x=(My Desktop.x/Resized Image1.x)*相对坐标.x;y=(My Desktop.y/调整大小的图像1.y)*相对坐标.y;对不起,输入错误,我已经修复了,是的,我确定找到了图像,我在MSPaint中看到了坐标,我需要澄清的是,AForge给我的坐标对应于切割图像的左上角found Bigger/Small不明确=原始桌面是您搜索的较小桌面的4倍,因此,使用因子4。我想知道它是否找到了不同比例的图像-按X%调整桌面大小,按Y%调整要查找的图像大小,看看是否找到了it@Plutonix我将相对坐标检索到一个
矩形
,因此也许我可以使用一个矩形的
膨胀
方法来简化事情?。。。但是数学不是我的。当它被解释得像你一样好的时候,它很容易理解,谢谢。