首页 > 解决方案 > 在powershell中截取整个可滚动wpf listview的屏幕截图?

问题描述

我有一个带有自定义图标和图像的 Powershell WPF 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, 0, 1000, 900)
Get-Screenshot -Bereich $Bereich 

找到了一些类似的文章获取可滚动视图的整个“屏幕截图”, 但没有“仅 powershell”方式的代码。

标签: .netwpfpowershelllistviewscreenshot

解决方案


另一种方法:我将列表视图转换为 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

推荐阅读