Jump to content
Sign in to follow this  
Viszna

How convert image in memory to PNG _ScreenCapture

Recommended Posts

Hello.

I create a function that saves the log to an html file.
File html are included picture (conversion to Base64)

Everything works OK.
But I do the actions:

  • screenshot to the png file (smaller than bmp) per disk (  _ScreenCapture_Capture(@ScriptDir & "\screenshot.png")  )
  • convert image from disk to base64

I need help to optimize the script:

  1. - screenshot to memory (do not save to disk)
  2. - convert this object to png in memory
  3. - invoking the conversion of png image from memory to base64

The first step _ScreenCapture_Capture("") create handle to an HBITMAP in memory
How to convert image in memory to png?
How to use Func _ConvertToBase64

I attach my code

#include <ScreenCapture.au3>
#include <Date.au3>


Global $RaportFileName = @YEAR & "-" & @MON & "-" & @MDAY & "_" & @HOUR & "_" & @MIN & "_" & @SEC & ".html"

$text = "This is first line text" & @CRLF &"and this is next line"

_Raport($text, 1)
FileWrite(@ScriptDir & "\" & $RaportFileName, "</pre></html>")



; #FUNCTION# ====================================================================================================================
; Name ..........: _Raport
; Description ...:
; Syntax ........: _Raport($sText1[, $Screen = 0])
; Parameters ....: $sText1              - a string value.
;                  $Screen              - [optional] an unknown value. Default is 0.
;                                          0 - Default - do not screenshot
;                                          1 - added screenshot full desktop
; Return values .: None
; Author ........: Your Name
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Raport($sText1, $Screen=0)
    Local $sText = ""
    Local $sHead = ""
    ; Define HTML file header and style
    $sHead = '<html>' & @CRLF & '<head><meta charset="utf-8"></head>' & @CRLF
    $sHead = $sHead & '<style>img{border:3px solid #FF0000;}</style>' & @CRLF
    $sHead = $sHead & '<style>pre{font-family: monospace;}</style>' & @CRLF
    $sHead = $sHead & '<style>pre{font-size: large;}</style>' & @CRLF
    $sHead = $sHead & '<pre>' & @CRLF

    If NOT FileExists(@ScriptDir & "\" & $RaportFileName) Then ; If file Raport not exist then create
        FileOpen(@ScriptDir & "\" & $RaportFileName, 258)
        FileWrite(@ScriptDir & "\" & $RaportFileName, $sHead)
    EndIf
    If StringInStr($sText1, @CRLF) > 0 Then ; @CRLF (ENTER) change the @CRLF and 11 space (indentation on width "[GG:MM:SS] ")
        $sText1 = StringReplace($sText1, @CRLF, @CRLF & '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
    EndIf

    $sText = $sText & $sText1
    If $Screen <> 0 Then
        _ScreenCapture_Capture(@ScriptDir & "\screenshot.png")
        $sText = $sText & @CRLF & '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' & '<img src="data:image/png;base64,' & _ConvertToBase64(@ScriptDir & "\screenshot.png") & '"/>'
    EndIf

    FileWrite(@ScriptDir & "\" & $RaportFileName, "[" & _NowTime(5) & "]&nbsp;" & $sText & "<br><br>"&@CRLF)    ; write to file Raport
EndFunc

Func _ConvertToBase64($fFile)
    ;Xroot 2011
    ;ClipPut("")
    ;$FN=@ScriptDir & "\screenshot.png"
    $FN=$fFile
    $dat=FileRead(FileOpen($FN,16))
    $objXML=ObjCreate("MSXML2.DOMDocument")
    $objNode=$objXML.createElement("b64")
    $objNode.dataType="bin.base64"
    $objNode.nodeTypedValue=$dat
    ClipPut("")
    $Wynik = ""
    ;ClipPut($objNode.Text)
    $Wynik = $objNode.Text
    Return $Wynik
EndFunc

P.S.
Excuse me my not good English.

Share this post


Link to post
Share on other sites

Try this :

#include <ScreenCapture.au3>
#include <GDIPlus.au3>
#include <Date.au3>
#include <Memory.au3>

Global $RaportFileName = @YEAR & "-" & @MON & "-" & @MDAY & "_" & @HOUR & "_" & @MIN & "_" & @SEC & ".html"

$text = "This is first line text" & @CRLF &"and this is next line"

_Raport($text, 1)
FileWrite(@ScriptDir & "\" & $RaportFileName, "</pre></html>")

; #FUNCTION# ====================================================================================================================
; Name ..........: _Raport
; Description ...:
; Syntax ........: _Raport($sText1[, $Screen = 0])
; Parameters ....: $sText1              - a string value.
;                  $Screen              - [optional] an unknown value. Default is 0.
;                                          0 - Default - do not screenshot
;                                          1 - added screenshot full desktop
; Return values .: None
; Author ........: Your Name
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Raport($sText1, $Screen=0)
    Local $sText = ""
    Local $sHead = ""
    ; Define HTML file header and style
    $sHead = '<html>' & @CRLF & '<head><meta charset="utf-8"></head>' & @CRLF
    $sHead = $sHead & '<style>img{border:3px solid #FF0000;}</style>' & @CRLF
    $sHead = $sHead & '<style>pre{font-family: monospace;}</style>' & @CRLF
    $sHead = $sHead & '<style>pre{font-size: large;}</style>' & @CRLF
    $sHead = $sHead & '<pre>' & @CRLF

    If NOT FileExists(@ScriptDir & "\" & $RaportFileName) Then ; If file Raport not exist then create
        FileOpen(@ScriptDir & "\" & $RaportFileName, 258)
        FileWrite(@ScriptDir & "\" & $RaportFileName, $sHead)
    EndIf
    If StringInStr($sText1, @CRLF) > 0 Then ; @CRLF (ENTER) change the @CRLF and 11 space (indentation on width "[GG:MM:SS] ")
        $sText1 = StringReplace($sText1, @CRLF, @CRLF & '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
    EndIf

    $sText = $sText & $sText1
    If $Screen <> 0 Then
        Local $hBitMap = _ScreenCapture_Capture("")
        _GDIPlus_Startup()
        Local $hImage = _GDIPlus_BitmapCreateFromHBITMAP ($hBitMap)
        Local $bString = Image2BinaryString($hImage)
        $sText = $sText & @CRLF & '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' & '<img src="data:image/png;base64,' & _
          _ConvertToBase64($bString) & '"/>'
        _GDIPlus_ImageDispose($hImage)
        _WinAPI_DeleteObject($hBitMap)
        _GDIPlus_Shutdown()

    EndIf

    FileWrite(@ScriptDir & "\" & $RaportFileName, "[" & _NowTime(5) & "]&nbsp;" & $sText & "<br><br>"&@CRLF)    ; write to file Raport
EndFunc

Func _ConvertToBase64($bString)
    $objXML=ObjCreate("MSXML2.DOMDocument")
    $objNode=$objXML.createElement("b64")
    $objNode.dataType="bin.base64"
    $objNode.nodeTypedValue=Binary($bString)
    Return $objNode.Text
EndFunc

Func Image2BinaryString($hBitmap) ; based on UEZ code
Local $sImgCLSID, $tGUID, $tParams, $tData

  $sImgCLSID = _GDIPlus_EncodersGetCLSID("PNG")
  $tGUID = _WinAPI_GUIDFromString($sImgCLSID)

  Local $hStream = _WinAPI_CreateStreamOnHGlobal()
  _GDIPlus_ImageSaveToStream($hBitmap, $hStream, DllStructGetPtr($tGUID))
  Local $hMemory = _WinAPI_GetHGlobalFromStream($hStream)
  Local $iMemSize = _MemGlobalSize($hMemory)
  Local $pMem = _MemGlobalLock($hMemory)
  $tData = DllStructCreate("byte[" & $iMemSize & "]", $pMem)
  Local $bData = DllStructGetData($tData, 1)
  _WinAPI_ReleaseStream($hStream)
  _MemGlobalFree($hMemory)
  Return $bData

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
Sign in to follow this  

  • Similar Content

    • By Colduction
      Hello AutoIt Scriptwriters! 
      I want to read https based site that it's address is: Soft98 (https://soft98.ir/)
      I've tried with "_INetGetSource", "BinaryToString(InetRead)" and "InetRead" but none of them don't help me
       
      How can i get this site html source code without opening IE Windows? 
       
    • By Arlen
      I need to find a way to get the HTML from a website that has HTTPS. This is what I have tried:
      - WinHttp (Only worked on HTTP NOT HTTPS)
      - InetGet and _INetGetSource Function (Not working for HTTPS)
      - IE.au3 (Does work but it's too slow for my needs)
      If anybody can point me on the right direction, I would really appreciate it.
       
    • By XaelloNegative
      Hi,
      So, I am trying to automate and simplify the retrieval of data from our company website (made by an outside company). I've had experience with IE manipulation via autoIt however, this one is a bit tricky for me.
      I have a table that has 81 cells which are buttons. What im trying to get is the information in "data-content" tag. 
      <a id="bookedVehicleLinkButton" class="btn btn-sm btn-block btn-xsm btn-success" data-toggle="popover" data-placement="bottom" data-original-title="Trip Details" data-container="body" data-content="Plate No: UGQ-857<br />Model: TOYOTA GRANDIA GL" href="javascript:__doPostBack('ctl00$ctl00$masterContentPlaceholder$reservationContentPlaceholder$bookingDataList$ctl00$vehicleDataList$ctl28$bookedVehicleLinkButton','')">IXARA (LITO SULIT)</a> Tried using the following:
      $oTagsCell = _IETagNameGetCollection($oIE, "td") For $item in $oTagsCell $test = _IEPropertyGet($item, "innertext") ClipPut($test) Next $oTagsTable = _IETagNameGetCollection($oIE, "table") For $item in $oTagsTable $test = _IEPropertyGet($item, "innertext") ClipPut($test) Next $oTagsLink = _IETagNameGetCollection($oIE, "a") For $item in $oTagsLink $test = _IEPropertyGet($item, "innertext") ClipPut($test) Next I don't receive any errors from my aforementioned attempts however no luck in getting those "data-contents". Attached is a screenshot, I do the looking of elements in chrome but the codes are for IE. And if you might ask, the cells (or buttons) have the same ID.
       
      Thank you guys for any inputs.
       
    • By Jemboy
      Hi,
      At work we have some proprietary website, users have to login to.
      I have "made" an autoit executable to start IE, go the website, login, so the user do not have input their credentials every time.
      By NDA I am not allowed disclosed the URL of the website nor the login credentials
      So I made a fake website and an autoitscript to illustrate my question.
      #include <ie.au3> $oIE = _IECreate ("about:blank", 0, 1, 1, 1) $HWND = _IEPropertyGet($oIE, "hwnd") WinActivate ($HWND,"") WinSetState($HWND, "", @SW_MAXIMIZE) _IENavigate ($oIE, "http://demo.rkilinc.nl",1) The above start my demo website. The actual website has some links in the footer that I do not want most people click on.
      I contacted the developers of the website and they are thinking of making an option to configure what links to show in the footer, but they said it's not a high priority for them.
       
      I discovered, that by click F12 and deleting the <footer> element the footer is delete from the live page view (until the page is reloaded off course)
      I want to automate the removal of the footer, without using things like send().
      I tried getting the footer with _IEGetObjById and deleting it with _IEAction, but that didn't work.
      Does any one has an idea how I could delete the footer directly from view with an autoit script?
      TIA, Jem.
       
       

    • By ur
      Is there any UDF to remove all anchor tags <a> with a particular class (and also its sub elements completely) in a html document.
      Here the classes are browse and breadcrumbs
      Like in the below image.


       
      I am not able to find that option in IE.au3
       
      Please suggest.
×
×
  • Create New...