打开多个网站并截屏|GDI错误



我创建了一个PowerShell脚本,可以打开Excel电子表格中的几个网站,并对每个网站进行屏幕截图。

这是代码:

[Reflection.Assembly]::LoadWithPartialName("System.Drawing")
function Screenshot([Drawing.Rectangle]$bounds, $path) {
   $bmp = New-Object Drawing.Bitmap $bounds.width, $bounds.height
   $graphics = [Drawing.Graphics]::FromImage($bmp)
   $graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.size)
   $bmp.Save($path)
   $graphics.Dispose()
   $bmp.Dispose()
}
$data = Import-Excel 'C:TempHostNamesHostNames.xlsx'
$data | ForEach-Object {
   $website = $_
   $url=$website.Hostnames.Split(',')[0]
   Write-Host $website.Name $url
   $IE=new-object -com internetexplorer.application
   $IE.visible=$true
   $IE.FullScreen=$false
   $IE.ToolBar = $false
   $IE.StatusBar = $false
   $IE.MenuBar = $false
   $IE.AddressBar = $true
   $IE.Resizable = $true
   $IE.Top = 0
   $IE.Left = 577
   $IE.Width = 1024
   $IE.Height = 747
   $IE.navigate2( $url )
   $i=0
   While ( $IE.busy -eq $true ) { 
      Start-Sleep -s 1
      $i = $i + 1
      if ( $i -ge 20 ) { break }
   }
   $bounds = [Drawing.Rectangle]::FromLTRB($IE.Left, $IE.Top, $IE.Left + $IE.Width, $IE.Top + $IE.Height)
   $filename = "C:TempHostNamesurlshots"+ ($website.Name) +".png"
   Screenshot $bounds $filename
   $IE.Quit()
}

问题是,在每个网站运行后,我一直收到这个错误:

Exception calling "Save" with "1" argument(s): "A generic error occurred in GDI+."
At C:TempHostNamesHostNames View.ps1:9 char:4
+    $bmp.Save($path)
+    ~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ExternalException

任何想法都会很棒!

试试这个。。。(西奥指着(

我只是做了一些调整来支持自己,因为我不想做所有优秀的事情。

根据电脑屏幕上的内容,你还必须将浏览器带到from,所以,我的调整增加了这一点。

[void] [Reflection.Assembly]::LoadWithPartialName('System.Drawing')
[void] [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
[void] [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')

function Screenshot([Drawing.Rectangle]$bounds, $path) 
{
   $bmp = New-Object Drawing.Bitmap $bounds.width, $bounds.height
   $graphics = [Drawing.Graphics]::FromImage($bmp)
   $graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.size)
   $bmp.Save($path)
   $graphics.Dispose()
   $bmp.Dispose()
}
# Open the browser once
$IE = New-Object -ComObject internetexplorer.application
$IE.visible    = $true
$IE.FullScreen = $false
$IE.ToolBar    = $false
$IE.StatusBar  = $false
$IE.MenuBar    = $false
$IE.AddressBar = $true
$IE.Resizable  = $true
$IE.Top        = 0
$IE.Left       = 577
$IE.Width      = 1024
$IE.Height     = 747
$i = 0
While ( $IE.busy -eq $true ) 
{ 
    Start-Sleep -Seconds 1
    $i = $i + 1
    if ( $i -ge 20 ) 
    { break }
}
# Read and process the data in the file
# Import-Excel -Path 'C:TempHostNamesHostNames.xlsx' 
# Set focus on the browser
[Microsoft.VisualBasic.Interaction]::AppActivate('Internet Explorer')
'microsoft.com','google.com','stackoverflow.com' |  
ForEach-Object {
<#
   $website = $_
   # Open an new site in a new tab, set focus on the tab
   $url = $website.Hostnames.Split(',')[0]
#>
   "Processing the website names $PSItem"
   $IE.navigate2( $PSItem )
   $bounds = [Drawing.Rectangle]::
   FromLTRB($IE.Left, $IE.Top, $IE.Left + $IE.Width, $IE.Top + $IE.Height)
   $filename = "D:Temp$($PSItem -replace '.com').png"
   # Take a screenshot
   Screenshot $bounds $filename
   Start-Sleep -Seconds 3
}
# Release resources
$IE.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($IE) | 
Out-Null 
[System.GC]::Collect() 
[System.GC]::WaitForPendingFinalizers()
Get-ChildItem -Path 'D:temp' -Filter '*.png'
<#
# Results
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        05-Mar-20     11:45         172410 google.png
-a----        05-Mar-20     11:45          17781 microsoft.png
-a----        05-Mar-20     11:45          50634 stackoverflow.png
#>

最新更新