Jump to content

Recommended Posts

Posted

Hi.

I am working on an older script of mine and I have troubles figuring out the correct coordinates for taking a screenshot of a certain region. The region lies within an image that is displayed by Autoit. I believe that the reason why the coordinates don't work anymore as earlier is that the script is running on a new computer with different monitor resolution.

Since it would take me quite some time to create a reproducer-script, I prefer to ask you for ideas first. So the following snippets show the crucial commands only.

Opt("PixelCoordMode", 1) ; 1 = (default) absolute screen coordinates

;Loading a (resized) image
$hImageresized = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\resized\" & $Path[3] & $Path[4])

;Retrieve image dimensions
$iWidth = _GDIPlus_ImageGetWidth($hImageresized)
$iHeight = _GDIPlus_ImageGetHeight($hImageresized)

$centerX = Round((@DesktopWidth * 1 / 2) - ($iWidth / 2), 0)
$centerY = Round((@DesktopHeight * 1 / 2) - ($iHeight / 2), 0)

$hGUIimage = GUICreate("GDI+ Example (" & @ScriptName & ")", $iWidth, $iHeight, $centerX, $centerY, BitOR($WS_BORDER, $WS_POPUP), BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW)) ;create GUI
    GUISetState(@SW_SHOW)
;[...]
_GDIPlus_GraphicsDrawImageRect($hGraphicGUI, $hBM, 0, 0, $iWidth, $iHeight)


;The user marks two point of interest in the image. The corrdinates are stored as follows: 
GetPos("Click Start Point")
            $Pt1[0] = $pos[0]
            $Pt1[1] = $pos[1]
            Sleep(300)

GetPos("Click End Point")
            $Pt2[0] = $pos[0]
            $Pt2[1] = $pos[1]
            
;A line between between the two points is drawn. Happening in a subroutine:
_DrawUserLine($Pt1[0] - $centerX, $Pt1[1] - $centerY, $Pt2[0] - $centerX, $Pt2[1] - $centerY, 3) 

;In the sub:
_GDIPlus_GraphicsDrawLine($hGraphicGUI, $Pt10, $Pt11, $Pt20, $Pt21, $hPen)

Until here, everything works fine. The relevant values:

  1. Desktopwidth = 2194
  2. DesktopHeight = 1234
  3. centerX = 732
  4. centerY = 67
  5. iHeight = 1101
  6. iWidth: 731
  7. PT1[0] = 735 (top left corner of the image)
  8. PT1[1] = 68 (top left corner of the image)
  9. PT2[0] = 1144 (about the center of the image and the screen - very roughly)
  10. PT2[1] = 712 (about the center of the image and the screen - very roughly)

But i struggle with the following part: Taking a screenshot, using the 2 points as boundaries (in addition to 30 pixels on each side). The snippet below expects that the first point is further top-left than the second point. Let's take that for granted.

If $Pt1[0] <= $Pt2[0] And $Pt1[1] <= $Pt2[1] Then       
        _ScreenCapture_Capture(@ScriptDir & "\closeup.jpg", $Pt1[0] - 30, $Pt1[1] - 30, $Pt2[0] + 30, $Pt2[1] + 30, False)
ElseIF 
    ;[...]

I tried by now all kinds of combinations for the values of Pt1 and Pt2. It is my understandig that I have to add $centerX and $centerY to the Coordinates. Obviously wrong. I am quite confused... probably missing the obvious.  I also changed the PixelCoordMode to 0 and 1. The latter had no impact at all. 

Thanks for your help.

Posted
23 minutes ago, Nine said:

Are you using a system magnifier zoom (like 125% or 150%) ?

Yes, that's it. In Windows 10, I have increased the size of "text apps and other items". Thank you very much, Nine!

So, I guess I need to change the magnifier from 175% to 100% - and back again. Will look into it.

Enjoy your weekend!

Posted
2 hours ago, Nine said:

What I did if you are like me and find it too small at 100%, I change the base resolution of the screen, and now everything is great again ;)

I like the way you think! But did I really pay that much money for 3 x 4k Screens to decrease the resolution in order to make Autoit work?

Wee, I'll consider it! Thanks for the hint!

Posted

I personally do not see much difference between changing resolution and having all my apps running at 150% zoom.  But I know it is not the same.  Bottom line, it will all depend how important is running your scripts right now compare to spend much time adapting your script to be DPI aware and delay your production. 

Posted

Just an idea: Is there an API call you can make to get the current magnification ratio from Windows during startup of your code, before it displays anything, and then plug it into your code as a variable multiplier everywhere you calculate screen co-ordinates?

That would then possibly make your code universally deployable.

Posted

You could automate this on a virtual computer with the resolution you want. But yeah you will spend time what ever you choose to do. 

So, if it was me, I would have done the script rework.

Anyway, this has been calculated one time, so it's possible to calculate this a second time.

And encounter that kind of problem will learn you that pixel things or coordinate things are not really reliable until you control these elements in your script. 

What I do when I do that kind of things into my script, it's positioning my script's GUI, or the target windows. Always at the same place. Then after I do my calculation. 

Then you make sure that position/size will always be the same when you need it.

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Posted
6 minutes ago, caramen said:

Anyway, this has been calculated one time, so it's possible to calculate this a second time.

 

My take was you should do this every time for robust operation. You could add this easily to your code.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...