LegitStack

ScreenCapture 4k res - how to get dpi awareness?

6 posts in this topic

#1 ·  Posted (edited)

Been struggling with this one for a while. 

when I do a _screencapture_capture call on a high resolution monitor (like my surface book) it gives me an image that has 2 problems:

1. its in the wrong location on the screen

and

2. it gives me a picture that is larger than the area of the screen I selected, though it only has the content of what I selected. 

---------------------------

I was able to easily fix problem #1 by manually adjusting the x y coordinates to compensate for the amount of DPI scale I have.

for instance if I'm 200% zoomed in the code looks like this:

Local $bmp = _ScreenCapture_Capture("", $iX1*2, $iY1*2, $iX2*2, $iY2*2, false)

 it's problem #2 that is the big problem. I'd like to attach a screen shot of what I'm talking about (see capture.png)

Capture.PNG

---------------------------

Now I basically understand why this is happening. ScreenCapture grabs each pixel of the screen. This screen, being a high resolution, when its zoomed in adds up several pixels to make one on the screen. 

This is a problem for me because I'm taking images of the screen and later looking for those exact images on the screen. if everything is blown up by an indeterminate amount (in my case 2x) then those images can't be found later on. 

Does anyone know what I can do? 

I tried resizing the images back down to no avail. 

_GDIPlus_ImageResize and _GDIPlus_ImageScale don't work because they don't compress the pixels correctly. quality is lost. and the exact image isn't preserved, so I can't search for it later. (see capture1.png)

Capture1.PNG

 

Anyway, I'm about to give up, been on this problem for too long! does anyone know what I can do? Seems to me that the ideal solution would be to eventually have autoit add an argument to _screencapture_capture that lets you specify a DPI scale amount or something.  that can be pulled from the registry at HKEY_CURRENT_USER\control panel\desktop\windowmetrics\appliedDPI.

But in the meantime, does anyone have any suggestions for how I can make my program compatible with 4k resolution monitors? I either need to take the screen capture like normal, then scale it down appropriately without losing quality, or I need to capture the screen in the first place like the human sees it. But I don't know how to do that either. 

I'll post my relevant code here: (in my project I call ScreenCapture_DPI_Aware)

#include <Security.au3>

Func _GetAppliedDPI()
  Local $aArrayOfData = _Security__LookupAccountName(@UserName)

  If IsArray($aArrayOfData) Then
    ;msgbox(64, "SID String = ", $aArrayOfData[0] & @CRLF)
    ;msgbox(64, "Domain name = ", $aArrayOfData[1] & @CRLF)
    ;msgbox(64, "SID type = ", _Security__SidTypeStr($aArrayOfData[2]) & @CRLF)
    ;Local $AppliedDPI = RegRead("HKEY_USERS\" & $aArrayOfData[0] & "\Control Panel\Desktop\WindowMetrics", "AppliedDPI")
    Local $AppliedDPI = RegRead("HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics", "AppliedDPI")
    return $AppliedDPI
  EndIf
EndFunc



Func GetScale()
  $applied = _GetAppliedDPI()
  if $applied == "" then
    return 1
  else
    return $applied / 96
  EndIf
EndFunc



Func ScreenCapture_Capture_DPI_Aware($sBMP_Path, $iX1, $iY1, $iX2, $iY2, $bool)
  $R = GetScale() ;Raito

  Local $bmp = _ScreenCapture_Capture($sBMP_Path, $iX1*$R, $iY1*$R, $iX2*$R, $iY2*$R, $bool)

  ;Scaling didn't work:
  ;_ScaleImage($bmp, $sBMP_Path, abs($iX2 - $iX1), abs($iY2 - $iY1), $R)
  ;return _ScreenCapture_Capture($sBMP_Path, $iX1*$R, $iY1*$R, $iX2*$R, $iY2*$R, $bool)
  
EndFunc







;Func _ScaleImage($bmp, $outimage, $w, $h, $scale)

;  _GDIPlus_Startup()
    ;Get the encoder of to save the resized image in the format you want.
;    Local $Ext = StringUpper(StringMid($outimage, StringInStr($outimage, ".", 0, -1) + 1))
;    $CLSID = _GDIPlus_EncodersGetCLSID($Ext)
    ; code found here : https://www.autoitscript.com/autoit3/docs/libfunctions/_GDIPlus_ImageSaveToStream.htm
;    Local $sImgCLSID = _GDIPlus_EncodersGetCLSID("png") ;create CLSID for a JPG image file type
;    Local $tGUID = _WinAPI_GUIDFromString($sImgCLSID) ;convert CLSID GUID to binary form and returns $tagGUID structure
;    Local $tParams = _GDIPlus_ParamInit(1) ;initialize an encoder parameter list and return $tagGDIPENCODERPARAMS structure
;    Local $tData = DllStructCreate("int Quality") ;create struct to set JPG quality setting
;    DllStructSetData($tData, "Quality", 100) ;quality 0-100 (0: lowest, 100: highest)
;    Local $pData = DllStructGetPtr($tData) ;get pointer from quality struct
;    _GDIPlus_ParamAdd($tParams, $GDIP_EPGQUALITY, 1, $GDIP_EPTLONG, $pData) ;add a value to an encoder parameter list

;    Local $gbmp = _GDIPlus_BitmapCreateFromHBITMAP($bmp)
;    _WinAPI_DeleteObject($bmp)

;    Local $gsbmp = _GDIPlus_ImageResize($gbmp, $w * $scale, $h * $scale)
    ;Local $ext = _GDIPlus_EncodersGetCLSID("PNG")
;    _GDIPlus_ImageSaveToFileEx($gsbmp, $outimage, $sImgCLSID)

;    _GDIPlus_BitmapDispose($gbmp)
;    _GDIPlus_BitmapDispose($gsbmp)
;  _GDIPlus_Shutdown()
;EndFunc


Thanks for any help you can give me!!!

Edited by LegitStack

Share this post


Link to post
Share on other sites



2 hours ago, RTFC said:

I'm having a deja-lu:blink:

Thanks RTFC!

this is what I came up with to fix the prob:

#include <lib\applieddpi.au3>


Func Load_ScreenCapture_High_DPI_Check()
  $R = GetScale()
  if $R == 1 then
    ; should fix this to work with dual monitors. later.
  else
    #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
    #AutoIt3Wrapper_Res_HiDpi=Y
    #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
    DllCall("User32.dll","bool","SetProcessDPIAware")
    ;(work around) first screencapture doesn't seem to work right, so take one and throw away.
    _ScreenCapture_Capture("",0,0,@DesktopWidth,@DesktopHeight)
  endif
EndFunc

Load_ScreenCapture_High_DPI_Check()

;;;;;;;AppliedDPI.au3


#include <Security.au3>


Func _GetAppliedDPI()
  Local $AppliedDPI = RegRead("HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics", "AppliedDPI")
  return $AppliedDPI
EndFunc


Func GetScale()
  $applied = _GetAppliedDPI()
  if $applied == "" then
    return 1
  else
    return $applied / 96
  EndIf
EndFunc

 

 

Share this post


Link to post
Share on other sites
On 10/16/2016 at 2:36 PM, LegitStack said:

Thanks RTFC!

this is what I came up with to fix the prob:

#include <lib\applieddpi.au3>


Func Load_ScreenCapture_High_DPI_Check()
  $R = GetScale()
  if $R == 1 then
    ; should fix this to work with dual monitors. later.
  else
    #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
    #AutoIt3Wrapper_Res_HiDpi=Y
    #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
    DllCall("User32.dll","bool","SetProcessDPIAware")
    ;(work around) first screencapture doesn't seem to work right, so take one and throw away.
    _ScreenCapture_Capture("",0,0,@DesktopWidth,@DesktopHeight)
  endif
EndFunc

Load_ScreenCapture_High_DPI_Check()

;;;;;;;AppliedDPI.au3


#include <Security.au3>


Func _GetAppliedDPI()
  Local $AppliedDPI = RegRead("HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics", "AppliedDPI")
  return $AppliedDPI
EndFunc


Func GetScale()
  $applied = _GetAppliedDPI()
  if $applied == "" then
    return 1
  else
    return $applied / 96
  EndIf
EndFunc

 

 

New to using computers with High Resolution screens...    and trying to get my scripts to adjust similar problems to this thread.  I'm having difficulty getting autoit to locate 

#include <lib\applieddpi.au3>

what file or location would I have it search for that?  thank you for entertaining a noob question... never had issues before  guessing the script itself isnt saved in the right location to find the file. 

 

 

Share this post


Link to post
Share on other sites

@JennMaughan: AFAIK, that's not a standard AutoIt include file, but probably something user LegitStack put together. You should be alright just copying only those functions to your own script(s), if you wish to use them.:)

Share this post


Link to post
Share on other sites

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

  • Similar Content

    • sree161
      By sree161
      Hi,
      i have a code which generates two numeric outputs.i have to save them in excel.
      Is there any way to export them to specific cells in excel???
    • TotalAuto
      By TotalAuto
      Howdy All
      I've gone around and around in circles wasting hours trying to fix this problem to no avail so I thought I would try posting for help given there are some real smart people in this forum.
      Background
      I have 5 separate .au3 scripts that all do different basic tasks. For example:
      1.au3 = opens Chrome browser to obtain information from a specific website then closes
      2.au3 = opens an Excel workbook to run a macro then saves and closes
      3.au3 = Open two seperate workbooks and transfers data from one to the other then saves and closes
      4.au3 = .......etc
      5.au3 = .......etc
      These 5 separate scripts, doing completely different tasks, need to each run once per day at 5 different designated times within a day and then close. For example:
      1.au3 = 10am
      2.au3 = 12pm
      3.au3 = 2pm
      4.au3 = 4pm
      5.au3 = 5pm
      Problem
      I can not get these different scripts to reliably run at their designated times. When I manually run these scripts individually by using 'Go F5' they work perfectly. If I convert these scripts to an .exe and manually run each one they work about 98% of time.
      How can I schedule these different .au3 files or associated .exe files to reliably run each day at different times?
      Attempts To Fix The Issue
      1. I have tried converting each .au3 script into their own .exe file and use Window's Task Scheduler to run these .exe files at their designated time but the success rate  of running each .exe in full, to completion, without error is very low at about 30%. Things that commonly go wrong include a browser not opening but movement still continues on the desk top. Other times things just do not happen at all.
      2. I have Windows 7 64 bit and have tried creating and running both (x86) and (x64) .exe files but again reliability problems exist;
      3. I have also tried moving each .exe file to the My Documents folder as I read someone that keeping your files on your desktop ties your files to individual Windows user profiles which can cause AutoIt operating errors. Not sure if this is correct but thought I would try it anyway. It did not fix my issue.
      4. I installed a program called AlwaysUp which supposedly always reliably runs in the background and will reliably execute your AutoIt script or .exe files. Still have significant reliability issues.
      Therefore, can someone please provide advice on how I can get my 5 different .au3 files to run at 5 designated times through the day, every day, without fail? Overtime I will be creating more and more different .au3 files to run certain tasks throughout the day and do not want to create one large AutoIt file that continually runs and attempts to do all the different tasks itself.
      Any help is very appreciated.
      Thanks
      Dan
       
       
    • hugomito
      By hugomito
      Hi all,
      I built an AutoIt application that interacts with several other applications/software. Now, it's turn to interfact with Visual Studio Team Services (VSTS).
      Does anyone has a blog, demo, script to share?
      Thanks!
    • Viki
      By Viki
      This is my first time here so please dont bombard me that what a silly question I am asking!!
      I have 500 rows (A1:A500) in a spreadsheet and I just want to copy one by one row and then paste into another application and then press enter, loop should repeat this until finishes all 500 rows.
      I have looked at clipget(), clip(put() but dont know how to select next row in next turn. I also looked at Array to store but again no luck. Can some guide me please..
    • DineshPawar
      By DineshPawar
      0down votefavorite   In my current project, Excel macro open AutoIt script using a function Call Shell("location of autoit script").
      For proper functioning of AutoIt script it's need to be close parent Excel workbook, so this closing Excel action is written in AutoIt script itself.
      But as soon as AutoIt script close parent Excel workbook then script get pause and it do nothing.
      How open the AutoIt script from Excel workbook and after that parent Workbook get close?