Sometimes we just want to take some screenshots of particular areas from within a PowerShell script. As part of some automation script, I created a function that will grab a part of the screen and optionally add a timestamp to it. You can save this to either the clipboard and/or a file.

To get a timestamp on the screenshot, we will load in the System.Drawing assembly, which gives us access to GDI+ functions. This allows us to draw text and all sorts of shapes.

The Get-Screenshot function

function Get-Screenshot 
{
    [CmdletBinding()]
    Param(
        [Parameter(Position=0, Mandatory=$true)]
        [int]$Left,
        [Parameter(Position=1, Mandatory=$true)]
        [int]$Top,
        [Parameter(Position=2, Mandatory=$true)]
        [int]$Right,
        [Parameter(Position=3, Mandatory=$true)]
        [int]$Bottom,
        [Parameter(Position=4, Mandatory=$false)]
        [string]$FileName,
        [Parameter(Position=5, Mandatory=$true)]
        [bool]$SaveToClipboard,
        [Parameter(Position=6, Mandatory=$false)]
        [bool]$AddTimestamp=$false
    )

    #Load the required .NET assembly.
    [Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null

    #Create our bounding box rectangle
    $BoundingBox = [Drawing.Rectangle]::FromLTRB($Left,$Top,$Right,$Bottom);

    #Create a new bitmap object in memory.
    $objBitmap = New-Object System.Drawing.Bitmap(($Right - $Left), ($Bottom - $Top));

    #Create a new graphics object based on our bitmap.
    $objGraphics = [Drawing.Graphics]::FromImage($objBitmap);

    #Copy the screen region to our graphics object.
    $objGraphics.CopyFromScreen($BoundingBox.Location, [Drawing.Point]::Empty, $BoundingBox.size)

    #If we want to add a timestamp text to our graphics object
    if($AddTimestamp)
    {
        #Set our font (fontfamily and size).
        $Timestamp_Font = New-Object System.Drawing.Font("Segoe UI Bold", 12);
        #Define the brushes for the foreground color and background color.
        $Timestamp_Brush_Fg = [System.Drawing.Brushes]::Red;
        $Timestamp_Brush_Bg = [System.Drawing.Brushes]::White;
        
        #Create a string with our current date and time.
        [String]$Timestamp_DateTimeString = Get-Date -UFormat "%d/%m/%Y %R";
        
        #Calculate the size of the background, based on our datetime string and our font.
        $Timestamp_Font_BackgroundSize = $objGraphics.MeasureString($Timestamp_DateTimeString, $Timestamp_Font);
        #Create a rectangleF object with the size of our text.
        $Timestamp_Font_BackgroundRect = New-Object System.Drawing.RectangleF(5,5, $Timestamp_Font_BackgroundSize.Width, $Timestamp_Font_BackgroundSize.Height);
       
        #Draw the background rectangle.
        $objGraphics.FillRectangle($Timestamp_Brush_Bg, $Timestamp_Font_BackgroundRect);
        #Draw the border for the rectangle.
        #Note: We have to use Round here to convert the RectangleF to Rectangle, as DrawRectangle does not have an overload for RectangleF.
        $objGraphics.DrawRectangle($Timestamp_Brush_Fg, [System.Drawing.Rectangle]::Round($Timestamp_Font_BackgroundRect));
        #Draw our text.
        $objGraphics.DrawString($Timestamp_DateTimeString, $Timestamp_Font, $Timestamp_Brush_Fg, 5, 5);      
    }

    #If we specified a file location.
    if($FileName -ne $null)
    {
        $objBitmap.Save($FileName);
    }

    #If we specified we want to save it in the clipboard.
    if($SaveToClipboard)
    {
        [Windows.Forms.Clipboard]::SetImage($objBitmap)
    }

    #Cleanup
    $objGraphics.Dispose()
    $objBitmap.Dispose()
}

We got quite a few parameters, but feel free to expand them. You could easily add some optional parameters to set the timestamp font color, size, position, etc…. The parameters should be pretty self explanatory.

-Left
The left position of the bounding box.
-Top
The top position of the bounding box.
-Right
The right position of the bounding box.
-Bottom
The bottom position of the bounding box.

-Filename
(optional) File you want to save the screenshot to.
-SaveToClipboard
(optional) Set to $true if you want to save to the clipboard. Default value is $false.
-AddTimestamp
(optional) Set to $true if you want to add a timestamp to the screenshot.

Usage

Get-Screenshot -Left 0 -Top 0 -Right 500 -Bottom 200 -FileName "C:\temp\scrtest.bmp" -SaveToClipboard $true -AddTimestamp $true;

Running the command resulted in:

Leave a Reply

Your email address will not be published. Required fields are marked *