LegitStack

ScreenCapture 4k res - how to get dpi awareness?

4 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

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

    • ExiLeD4EveR
      By ExiLeD4EveR
      'm trying to make an autologin script for facebook which prompts u to enter an email and a password and then opens the browser and logs you with the info u entered but im having a problem with the text entered in the input box. What i basically want it to do is take what's written on the inputbox and replace it on the email form in the facebook site and the password. Don't mind the other buttons in the gui , i just wanted to focus on the facebook button first. Any help will be appreciated
      #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <EditConstants.au3> #include <StaticConstants.au3> #include <IE.au3> #Region ### START Koda GUI section ### Form=c:\users\ExiLeD\desktop\coding\autoit\gui samples\simplegui.kxf $SimpleGUI = GUICreate("MultiProgram", 392, 122, -1, -1) GUISetIcon("C:\Users\ExiLeD\Desktop\Work\Smile\Icon Pack\EcranLcd.ico", -1) $Notepad = GUICtrlCreateButton("Notepad", 16, 24, 73, 25) GUICtrlSetCursor (-1, 0) $Calculator = GUICtrlCreateButton("Calculator", 112, 24, 73, 25) GUICtrlSetCursor (-1, 0) $ExitButoon = GUICtrlCreateButton("Exit", 160, 72, 73, 25) GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif") GUICtrlSetCursor (-1, 0) $Lol = GUICtrlCreateButton("Lol Login", 208, 24, 73, 25) GUICtrlSetCursor (-1, 0) $Facebook = GUICtrlCreateButton("Facebook ", 304, 24, 73, 25) GUICtrlSetCursor (-1, 0) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Facebook ;= if Facebook is pressed it creates the email form $Face_Login = GUICreate("Facebook Login", 295, 88, -1, -1) GUISetIcon("C:\Users\ExiLeD\Downloads\Iconshock-Social-Media-Beakers-Facebook.ico", -1) $Label1 = GUICtrlCreateLabel("Email", 40, 24, 29, 17) $InputBox1 = GUICtrlCreateInput("", 72, 21, 148, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER)) $data1 = GUICtrlRead(-1) $Email_button = GUICtrlCreateButton("Done", 96, 56, 97, 25) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Email_button ;= if Done from email form is pressed then delete the email form and open the password form GUIDelete($Face_Login) Sleep(200) $Face_Login2 = GUICreate("Facebook Login", 295, 88, -1, -1) GUISetIcon("C:\Users\ExiLeD\Downloads\Iconshock-Social-Media-Beakers-Facebook.ico", -1) $Label2 = GUICtrlCreateLabel("Password", 16, 24, 50, 17) $InputBox2 = GUICtrlCreateInput("", 72, 21, 148, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER)) $data2 = GUICtrlRead(-1) $Pass_button = GUICtrlCreateButton("Done", 96, 56, 97, 25) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Pass_button ;= if Done from password form is pressed then it deletes the password form and , to add(open internet explorer on the facebook login page GUIDelete($Face_Login2) FB_SignIn() EndSwitch WEnd EndSwitch WEnd EndSwitch WEnd Func FB_SignIn() Global $oIE = _IECreate("https://www.facebook.com/") Local $username = _IEGetObjByName($oIE, "email") Local $password = _IEGetObjByName($oIE, "pass") Local $button = _IEGetObjById($oIE, "loginbutton") _IEFormElementSetValue($username, $data1) _IEFormElementSetValue($password, $data2) _IEAction($button, "click") EndFunc  
    • messilm10
      By messilm10
      how do i get the crash report of any application and any running script?????
    • ronaldo97
      By ronaldo97
      Hello How can I make the GUI is compatible with all screen sizes ??  
      #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 623, 445, 192, 124, BitOR($GUI_SS_DEFAULT_GUI,$WS_MAXIMIZE)) GUISetBkColor(0x1E1E1E) $Label1 = GUICtrlCreateLabel("Welcome Back !!", 424, 64, 161, 29) GUICtrlSetFont(-1, 16, 400, 0, "Tahoma") GUICtrlSetColor(-1, 0x800000) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd How do I make Gui interface compatible with all screen sizes ?? 800*600
      1024*768
      1280*1024
    • messilm10
      By messilm10
      respected sir,
      how could I calculate the time difference using auto IT function ?
    • Xandy
      By Xandy
      Waxworks Code Wheel.
      Written in AutoIt with SDL.
      Source