Aug 30, 2014 02:47 PM
(This post was last modified: Aug 30, 2014 03:09 PM by CottleSilver.)
A new thread was opened by ElementalTree on how to find an image's location from the screen and everyone sad that is difficult but in reality is not that hard so I want to show this simple code on how to do it:
1) use this code:
To be clear this is not 100% made by me more like 10%
PHP Code
<?php Private Function ImageLocation(ByRef Source As Bitmap, ByRef bmp As Bitmap) As Point If Source Is Nothing OrElse bmp Is Nothing Then Return Nothing If Source.Width = bmp.Width AndAlso Source.Height = bmp.Height Then If Source.GetPixel(0, 0) = bmp.GetPixel(0, 0) Then Return New Point(0, 0) Else Return Nothing End If ElseIf Source.Width < bmp.Width OrElse Source.Height < bmp.Height Then Return Nothing End If Dim sr As New Rectangle(0, 0, Source.Width, Source.Height) Dim br As New Rectangle(0, 0, bmp.Width, bmp.Height) Dim srcLock As BitmapData = Source.LockBits(sr, Imaging.ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb) Dim bmpLock As BitmapData = bmp.LockBits(br, Imaging.ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb) Dim sStride As Integer = srcLock.Stride Dim bStride As Integer = bmpLock.Stride Dim srcSz As Integer = sStride * Source.Height Dim bmpSz As Integer = bStride * bmp.Height Dim srcBuff(srcSz) As Byte Dim bmpBuff(bmpSz) As Byte Marshal.Copy(srcLock.Scan0, srcBuff, 0, srcSz) Marshal.Copy(bmpLock.Scan0, bmpBuff, 0, bmpSz) bmp.UnlockBits(bmpLock) Source.UnlockBits(srcLock) Dim x, y, x2, y2, sx, sy, bx, by, sw, sh, bw, bh As Integer Dim r, g, b As Byte Dim p As Point = Nothing bw = bmp.Width bh = bmp.Height sw = Source.Width - bw sh = Source.Height - bh bx = 0 : by = 0 For y = 0 To sh sy = y * sStride For x = 0 To sw sx = sy + x * 3 r = srcBuff(sx + 2) g = srcBuff(sx + 1) b = srcBuff(sx) If r = bmpBuff(2) AndAlso g = bmpBuff(1) AndAlso b = bmpBuff(0) Then p = New Point(x, y) For y2 = 0 To bh - 1 by = y2 * bStride For x2 = 0 To bw - 1 bx = by + x2 * 3 sy = (y + y2) * sStride sx = sy + (x + x2) * 3 r = srcBuff(sx + 2) g = srcBuff(sx + 1) b = srcBuff(sx) If Not (r = bmpBuff(bx + 2) AndAlso g = bmpBuff(bx + 1) AndAlso b = bmpBuff(bx)) Then p = Nothing sy = y * sStride Exit For End If Next If p = Nothing Then Exit For Next End If If p <> Nothing Then Exit For Next If p <> Nothing Then Exit For Next bmpBuff = Nothing srcBuff = Nothing Return p End Function
2) Just enjoy
Optional::
1) use this code to make a screenshot of the screen:
PHP Code
<?php Private Function Screenshot() As Image Dim bounds As Rectangle Dim IMG As System.Drawing.Bitmap Dim graph As Graphics bounds = Screen.PrimaryScreen.Bounds IMG = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb) graph = Graphics.FromImage(IMG) graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy) Return IMG End Function
2) Use both function to find a image from screen and use this to click the image:
PHP Code
<?php Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long) Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Long Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Long Dim MOUSEEVENTF_MIDDLEUP = &H40 Dim MOUSEEVENTF_RIGHTDOWN = &H8 Dim MOUSEEVENTF_LEFTDOWN = &H2 Dim MOUSEEVENTF_RIGHTUP = &H10 Dim MOUSEEVENTF_LEFTUP = &H4 Dim MOUSEEVENTF_MIDDLEDOWN = &H20
Private Sub ClickMouse(ByVal MouseEvent As String) If MouseEvent.ToLower = "left" Then mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) ElseIf MouseEvent.ToLower = "right" Then mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0) mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0) ElseIf MouseEvent.ToLower = "middle" Then mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0) mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0) End If End Sub
Use SetCursorPos and GetCursorPos to move the mouse. To make ClickMouse easier to use just make a enum and use select case, this is a old code
This is a link to the source only with 2 pictures (my own desktop) if anyone wants it: http://www.mediafire.com/download/wje45l...cation.rar
> [Tutorial]
> 1) C&P this code:
Nice tutorial OP, Amazing work.
(Aug 30, 2014 02:53 PM)$lick Wrote: > [Tutorial]
> 1) C&P this code:
Nice tutorial OP, Amazing work.
Changed, No one knows the meaning of a joke anymore....
HAHHAHAHAHAHAHAH FUNNY JOKE OP 14/10 would laugh again.
What was the joke?
(Aug 30, 2014 02:57 PM)CottleSilver Wrote: Changed, No one knows the meaning of a joke anymore....
Even if that was a joke you haven't fixed anything by removing "C&P this"
This still isn't a tutorial, It's a block of source you are telling is to use.
(Aug 30, 2014 03:00 PM)$lick Wrote: Even if that was a joke you haven't fixed anything by removing "C&P this"
This still isn't a tutorial, It's a block of source you are telling is to use.
What's wrong with copy paste code? If it works there is no point to reinvent the wheel.
(Aug 30, 2014 03:00 PM)$lick Wrote: Even if that was a joke you haven't fixed anything by removing "C&P this"
This still isn't a tutorial, It's a block of source you are telling is to use.
Well... I never did a tutorial before, I will change the title
(Aug 30, 2014 03:01 PM)banevader Wrote: What's wrong with copy paste code? If it works there is no point to reinvent the wheel.
Didn't say that was the issue.
The issue was calling this thread a tutorial.
(Aug 30, 2014 03:01 PM)CottleSilver Wrote: Well... I never did a tutorial before, I will change the title
There is a [Source] tag you could have used.
(Aug 30, 2014 03:07 PM)$lick Wrote: There is a [Source] tag you could have used.
Changed it to [Source] hope there are no more problems 🤝
|