在powershell中拍摄整个可滚动wpf listview的屏幕截图?

我有一个带有自定义图标和图像的Powershell WPF ListView。即使由于许多行而导致listview内容是可滚动的,也希望对整个listview进行截图(以捕获图标/图像)。

使用这样的代码,我只能截取特定区域(此处为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,1000,900)
Get-Screenshot -bereich $bereich 

找到了类似Take whole "Screenshot" of a scrollable View的文章 但没有“仅Powershell”方式的代码。

y4328978 回答:在powershell中拍摄整个可滚动wpf listview的屏幕截图?

另一种方法:我将listview转换为xml,然后可以添加CSS类并最终导出为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
本文链接:https://www.f2er.com/3113877.html

大家都在问