0

I have a Powershell WPF ListView with customized icons and images. Would like to take a screenshot of the whole listview (to catch also icons/images) even if listview content is scrollable because of a lot of rows.

With code like this I can only take a screenshot of a specific area (here 1000x900px).

Add-Type -Assembly System.Drawing
function Get-Screenshot
{
 param([System.Drawing.Rectangle]$Bereich)
 $Bmp = New-Object System.Drawing.Bitmap $Bereich.Width, $Bereich.Height
 $Graphics = [System.Drawing.Graphics]::FromImage($Bmp)
 $Graphics.CopyFromScreen($Bereich.Location, [System.Drawing.Point]::Empty,$Bereich.Size)
 $BmpPath = "$env:userprofile\documents\Screenshot_{0:dd}{0:MM}_{0:HH}_{0:mm}_{0:ss}.png" -f (Get-Date)
 $Bmp.Save($BmpPath)
 $Graphics.Dispose()
 $Bmp.Dispose()
}
$Bereich = [System.Drawing.Rectangle]::FromLTRB(0, 0, 1000, 900)
Get-Screenshot -Bereich $Bereich 

Found some articles like that Take whole "Screenshot" of a scrollable View but no code for "powershell only" way to do it.

1 Answers1

0

Another approach: I convert the listview to xml then I can add css classes and finally export as html:

# HTML formatting
$head = @"
<style>
BODY{background-color:white;}
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;foreground-color: black;background-color: LightBlue}
TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;foreground-color: black;background-color: white}
.status1{width:80px;height:18px;background-image: url('C:/Programdata/ZPTInfo/Icons/status1.gif');background-repeat:no-repeat;background-size:80px 18px;}
.status3{width:80px;height:18px;background-image: url('C:/Programdata/ZPTInfo/Icons/status3.gif');background-repeat:no-repeat;background-size:80px 18px;}
.status8{width:80px;height:18px;background-image: url('C:/Programdata/ZPTInfo/Icons/status8.gif');background-repeat:no-repeat;background-size:80px 18px;}
.status9{width:80px;height:18px;background-image: url('C:/Programdata/ZPTInfo/Icons/status9.gif');background-repeat:no-repeat;background-size:80px 18px;}
.status81{width:80px;height:18px;background-image: url('C:/Programdata/ZPTInfo/Icons/status81.gif');background-repeat:no-repeat;background-size:80px 18px;}
.status91{width:80px;height:18px;background-image: url('C:/Programdata/ZPTInfo/Icons/status91.gif');background-repeat:no-repeat;background-size:80px 18px;}
</style>
"@

$filedate=Get-Date -UFormat '%m-%d-%Y-%H%M%S'
$filename = "C:\Temp\export_" + $filedate + ".htm"
#$filename = "C:\Temp\export_.htm"

# Convert the selection to an XML object
[xml]$xmlObject = $WPFergebnisse.items | select-Object Ebene,Typ,Pos,Identnr,BENr,Bez,AGKurz,Menge,Status,letzterm,einkaufskz,zusatz | ConvertTo-Html -Fragment

# Parse XML object and set colour class according to value in 4th last column
for($i=1;$i -le $xmlObject.table.tr.count-1;$i++) {
    switch($xmlObject.table.tr[$i].td[-4] -as [int]){
        1 { $xmlObject.table.tr[$i].ChildNodes[($xmlObject.table.tr[$i].ChildNodes.Count-4)].SetAttribute('class','status1') }
        3 { $xmlObject.table.tr[$i].ChildNodes[($xmlObject.table.tr[$i].ChildNodes.Count-4)].SetAttribute('class','status3') }
        8 { $xmlObject.table.tr[$i].ChildNodes[($xmlObject.table.tr[$i].ChildNodes.Count-4)].SetAttribute('class','status8') }
        9 { $xmlObject.table.tr[$i].ChildNodes[($xmlObject.table.tr[$i].ChildNodes.Count-4)].SetAttribute('class','status9') }
        81 { $xmlObject.table.tr[$i].ChildNodes[($xmlObject.table.tr[$i].ChildNodes.Count-4)].SetAttribute('class','status81') }
        91 { $xmlObject.table.tr[$i].ChildNodes[($xmlObject.table.tr[$i].ChildNodes.Count-4)].SetAttribute('class','status91') }
    } 
}

# Define body and append the modified XML object
$body = @"
<H5>Generiert am:  $filedate</H5>
$($xmlObject.innerxml)
"@

# Convert to HTML and save the file
ConvertTo-Html -Head $head -Body $body | Out-File $filename