Jump to content
Sign in to follow this  
RAMMRODD

Screen Capture and time stamp

Recommended Posts

RAMMRODD

I'm trying to make the example script capture the screen, emboss "Created with Autoit 12/12/12" and then save it as something with the date in the name.

The purpose of this is to take a screenshot every time something happens and then save the file as a name that includes the time so you can see when it went wrong. Im trying to make a log of sorts that takes pictures during different steps of a process. I'm thinking the issue might be that the characters associated with time are not allowed in saving a file, but I'm not sure.

So any help would be great, thanks.

Ram

EDIT: Right after posting this I realized I could modify the _now and just stringreplace the colon and slashes with periods or dashes. New problem is making it save as "@MyDocumentsDir & '\'& $Time &'bmp'"

#include <GDIPlus.au3>
#include <ScreenCapture.au3>
#include<date.au3>

Opt("MustDeclareVars", 1)

Global $hBitmap, $hImage, $hGraphic, $hFamily, $hFont, $tLayout, $hFormat, $aInfo, $hBrush1, $hBrush2, $iWidth, $iHeight, $hPen
Global $sString=("  Created with AutoIt  "&_Now());<-----my simple change

_GDIPlus_StartUp()

; Capture screen
$hBitmap  = _ScreenCapture_Capture(@MyDocumentsDir & '\AutoItImage.bmp')

; Load image and emboss text
$hImage   = _GDIPlus_ImageLoadFromFile(@MyDocumentsDir & '\AutoItImage.bmp')
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
$hFamily  = _GDIPlus_FontFamilyCreate("Arial")
$hFont    = _GDIPlus_FontCreate($hFamily, 16, 1)
$tLayout  = _GDIPlus_RectFCreate(0, 0)
$hFormat  = _GDIPlus_StringFormatCreate(2)
$hBrush1  = _GDIPlus_BrushCreateSolid(0xA2FFFFFF)
$hBrush2  = _GDIPlus_BrushCreateSolid(0xC4FF0000)
$hPen     = _GDIPlus_PenCreate(0xC4000000, 2)
$aInfo    = _GDIPlus_GraphicsMeasureString($hGraphic, $sString, $hFont, $tLayout, $hFormat)
$iWidth   = DllStructGetData($aInfo[0], "Width" )
$iHeight  = DllStructGetData($aInfo[0], "Height")

_GDIPlus_GraphicsFillRect($hGraphic, 0, 0, $iWidth, $iHeight, $hBrush1)
_GDIPlus_GraphicsDrawRect($hGraphic, 1, 1, $iWidth, $iHeight, $hPen   )
_GDIPlus_GraphicsDrawStringEx($hGraphic, $sString, $hFont, $aInfo[0], $hFormat, $hBrush2)

; Save image
_GDIPlus_ImageSaveToFile($hImage, @MyDocumentsDir & '\AutoItImage2.bmp')

; Free resources
_GDIPlus_PenDispose         ($hPen    )
_GDIPlus_BrushDispose       ($hBrush1 )
_GDIPlus_BrushDispose       ($hBrush2 )
_GDIPlus_StringFormatDispose($hFormat )
_GDIPlus_FontDispose        ($hFont   )
_GDIPlus_FontFamilyDispose  ($hFamily )
_GDIPlus_GraphicsDispose    ($hGraphic)
_GDIPlus_ImageDispose       ($hImage  )
_GDIPlus_ShutDown()

; Show image
Run("MSPaint.exe " & '"' & @MyDocumentsDir & '\AutoItImage2.bmp"')
Edited by RAMMRODD

Share this post


Link to post
Share on other sites
Malkey

Try this.

#include <GDIPlus.au3>
#include <ScreenCapture.au3>
#include <date.au3>

Opt("MustDeclareVars", 1)

; =============================================================
; Description ...: Shows how to emboss text on an image
; Author ........: Paul Campbell (PaulIA)
; Notes .........: Modified
; ==============================================================
Global $hBitmap, $hImage, $hGraphic, $hFamily, $hFont, $tLayout, $FName
Global $hFormat, $aInfo, $hBrush1, $hBrush2, $iWidth, $iHeight, $hPen
Global $sString = " Created with AutoIt " & _Now()

_GDIPlus_Startup()

; Capture screen
$hBitmap = _ScreenCapture_Capture();@DesktopCommonDir & '\AutoItImage.bmp')
$hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)

; Emboss text
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
$hFamily = _GDIPlus_FontFamilyCreate("Arial")
$hFont = _GDIPlus_FontCreate($hFamily, 16, 1)
$tLayout = _GDIPlus_RectFCreate(0, 0)
$hFormat = _GDIPlus_StringFormatCreate(2)
$hBrush1 = _GDIPlus_BrushCreateSolid(0xA2FFFFFF)
$hBrush2 = _GDIPlus_BrushCreateSolid(0xC4FF0000)
$hPen = _GDIPlus_PenCreate(0xC4000000, 2)
$aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $sString, $hFont, $tLayout, $hFormat)
$iWidth = DllStructGetData($aInfo[0], "Width")
$iHeight = DllStructGetData($aInfo[0], "Height")

_GDIPlus_GraphicsFillRect($hGraphic, 0, 0, $iWidth, $iHeight, $hBrush1)
_GDIPlus_GraphicsDrawRect($hGraphic, 1, 1, $iWidth, $iHeight, $hPen)
_GDIPlus_GraphicsDrawStringEx($hGraphic, $sString, $hFont, $aInfo[0], $hFormat, $hBrush2)

; Save image
Local $FName = StringFormat("\A%2s%02d%02d_%02d%02d%02d.png", StringRight(@YEAR, 2), @MON, @MDAY, @HOUR, @MIN, @SEC)
_GDIPlus_ImageSaveToFile($hImage, @DesktopCommonDir & $FName)

; Free resources
_GDIPlus_PenDispose($hPen)
_GDIPlus_BrushDispose($hBrush1)
_GDIPlus_BrushDispose($hBrush2)
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_FontDispose($hFont)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_ImageDispose($hImage)
_WinAPI_DeleteObject($hBitmap)
_GDIPlus_Shutdown()

; Show image
ShellExecute(@DesktopCommonDir & $FName)
Edited by Malkey

Share this post


Link to post
Share on other sites
RAMMRODD

Thanks so much for the reply! I tried what you posted but it comes up with an error saying it cant find the file. I removed the "show image" part just to see if opening it was the problem and it was. I tried to examine your code to see if I could figure out what the problem might be but couldnt. I appreciate the work you put in though

thanks

Share this post


Link to post
Share on other sites
DW1

The only "problem" with the above script is the location he used for storage.

You can solve this by either adding "#RequireAdmin" to the top of the script, or just change the save and open directory from "@DesktopCommonDir & $FName" To something that doesn't require admin from UAC, like the desktop of the user for instance ("@DesktopDir & $FName").

Now the script should work as expected, with either solution.

The #RequireAdmin solution

#RequireAdmin
#include <GDIPlus.au3>
#include <ScreenCapture.au3>
#include <date.au3>

Opt("MustDeclareVars", 1)

; =============================================================
; Description ...: Shows how to emboss text on an image
; Author ........: Paul Campbell (PaulIA)
; Notes .........: Modified
; ==============================================================
Global $hBitmap, $hImage, $hGraphic, $hFamily, $hFont, $tLayout, $FName
Global $hFormat, $aInfo, $hBrush1, $hBrush2, $iWidth, $iHeight, $hPen
Global $sString = " Created with AutoIt " & _Now()

_GDIPlus_Startup()

; Capture screen
$hBitmap = _ScreenCapture_Capture();@DesktopCommonDir & '\AutoItImage.bmp')
$hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)

; Emboss text
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
$hFamily = _GDIPlus_FontFamilyCreate("Arial")
$hFont = _GDIPlus_FontCreate($hFamily, 16, 1)
$tLayout = _GDIPlus_RectFCreate(0, 0)
$hFormat = _GDIPlus_StringFormatCreate(2)
$hBrush1 = _GDIPlus_BrushCreateSolid(0xA2FFFFFF)
$hBrush2 = _GDIPlus_BrushCreateSolid(0xC4FF0000)
$hPen = _GDIPlus_PenCreate(0xC4000000, 2)
$aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $sString, $hFont, $tLayout, $hFormat)
$iWidth = DllStructGetData($aInfo[0], "Width")
$iHeight = DllStructGetData($aInfo[0], "Height")

_GDIPlus_GraphicsFillRect($hGraphic, 0, 0, $iWidth, $iHeight, $hBrush1)
_GDIPlus_GraphicsDrawRect($hGraphic, 1, 1, $iWidth, $iHeight, $hPen)
_GDIPlus_GraphicsDrawStringEx($hGraphic, $sString, $hFont, $aInfo[0], $hFormat, $hBrush2)

; Save image
Local $FName = StringFormat("\A%2s%02d%02d_%02d%02d%02d.png", StringRight(@YEAR, 2), @MON, @MDAY, @HOUR, @MIN, @SEC)
_GDIPlus_ImageSaveToFile($hImage, @DesktopCommonDir & $FName)

; Free resources
_GDIPlus_PenDispose($hPen)
_GDIPlus_BrushDispose($hBrush1)
_GDIPlus_BrushDispose($hBrush2)
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_FontDispose($hFont)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_ImageDispose($hImage)
_WinAPI_DeleteObject($hBitmap)
_GDIPlus_Shutdown()

; Show image
ShellExecute(@DesktopCommonDir & $FName)

Or the change directory solution:

#include <GDIPlus.au3>
#include <ScreenCapture.au3>
#include <date.au3>

Opt("MustDeclareVars", 1)

; =============================================================
; Description ...: Shows how to emboss text on an image
; Author ........: Paul Campbell (PaulIA)
; Notes .........: Modified
; ==============================================================
Global $hBitmap, $hImage, $hGraphic, $hFamily, $hFont, $tLayout, $FName
Global $hFormat, $aInfo, $hBrush1, $hBrush2, $iWidth, $iHeight, $hPen
Global $sString = " Created with AutoIt " & _Now()

_GDIPlus_Startup()

; Capture screen
$hBitmap = _ScreenCapture_Capture();@DesktopCommonDir & '\AutoItImage.bmp')
$hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)

; Emboss text
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
$hFamily = _GDIPlus_FontFamilyCreate("Arial")
$hFont = _GDIPlus_FontCreate($hFamily, 16, 1)
$tLayout = _GDIPlus_RectFCreate(0, 0)
$hFormat = _GDIPlus_StringFormatCreate(2)
$hBrush1 = _GDIPlus_BrushCreateSolid(0xA2FFFFFF)
$hBrush2 = _GDIPlus_BrushCreateSolid(0xC4FF0000)
$hPen = _GDIPlus_PenCreate(0xC4000000, 2)
$aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $sString, $hFont, $tLayout, $hFormat)
$iWidth = DllStructGetData($aInfo[0], "Width")
$iHeight = DllStructGetData($aInfo[0], "Height")

_GDIPlus_GraphicsFillRect($hGraphic, 0, 0, $iWidth, $iHeight, $hBrush1)
_GDIPlus_GraphicsDrawRect($hGraphic, 1, 1, $iWidth, $iHeight, $hPen)
_GDIPlus_GraphicsDrawStringEx($hGraphic, $sString, $hFont, $aInfo[0], $hFormat, $hBrush2)

; Save image
Local $FName = StringFormat("\A%2s%02d%02d_%02d%02d%02d.png", StringRight(@YEAR, 2), @MON, @MDAY, @HOUR, @MIN, @SEC)
_GDIPlus_ImageSaveToFile($hImage, @DesktopDir & $FName)

; Free resources
_GDIPlus_PenDispose($hPen)
_GDIPlus_BrushDispose($hBrush1)
_GDIPlus_BrushDispose($hBrush2)
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_FontDispose($hFont)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_ImageDispose($hImage)
_WinAPI_DeleteObject($hBitmap)
_GDIPlus_Shutdown()

; Show image
ShellExecute(@DesktopDir & $FName)
Edited by danwilli

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  

×