Winforms 通过ADB进行Powershell屏幕广播

Winforms 通过ADB进行Powershell屏幕广播,winforms,powershell,timer,adb,screencast,Winforms,Powershell,Timer,Adb,Screencast,我编写这个powershell脚本的目的是创建一个GUI,在这里我可以看到我的android屏幕投射到我的计算机上 该脚本创建了一个表单,其中计时器应使用从adb提取的图像更新picturebox。该脚本还确保安装了adb 我的问题是picturebox中的图像没有更新,它只是加载捕获的第一个图像 我相信这是adb没有覆盖现有png文件的问题,但我不确定,可能是其他原因 我将感谢任何帮助 Set-ExecutionPolicy Bypass -Scope Process -Force funct

我编写这个powershell脚本的目的是创建一个GUI,在这里我可以看到我的android屏幕投射到我的计算机上

该脚本创建了一个表单,其中计时器应使用从adb提取的图像更新picturebox。该脚本还确保安装了adb

我的问题是picturebox中的图像没有更新,它只是加载捕获的第一个图像

我相信这是adb没有覆盖现有png文件的问题,但我不确定,可能是其他原因

我将感谢任何帮助

Set-ExecutionPolicy Bypass -Scope Process -Force
function startTimer() { 
   $timer.start()
   Write-Host "Timer Started"
}
function stopTimer() {
    $timer.Enabled = $false
    Write-Host "Timer Stopped"
    $btnStart.Text = "Continue"
}
function TimerTick()
{
    adb shell rm -f /sdcard/sc.png
    adb shell screencap -p /sdcard/sc.png
    adb pull /sdcard/sc.png $BpathTo
    $image = [System.Drawing.Image]::Fromfile("$BpathTo\sc.png")
    $picb.Image = $image
    $lblLog.Text = (Get-Date).ToString()
    Write-Host "Refreshed at: "(Get-Date).ToString()
    Write-Host ""
}
if (!(Get-Command adb -ErrorAction SilentlyContinue)){
    if (!(Get-Command choco -ErrorAction SilentlyContinue)){
        Write-Host "Downloading Chocolatey Installer to setup ADB (required)"
        iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
    }
    Write-Host " Installing ADB..."
    choco install adb --force -y
}
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000
$timer.add_tick({TimerTick})

$BpathTo = (Get-Item -Path ".\").FullName
if ([System.IO.File]::Exists("$BpathTo\sc.png")){
    Remove-Item -Path "$BpathTo\sc.png"
}
adb shell screencap -p /sdcard/sc.png
adb pull /sdcard/sc.png $BpathTo
$image = [drawing.image]::FromFile("$BpathTo\sc.png")
$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "ADB Screencast"
$objForm.Size = New-Object Drawing.Size @(($image.Width+20),($image.Height+85))
$objForm.StartPosition = "CenterScreen"
$objForm.Add_KeyDown({
    if ($_.KeyCode -eq "Escape") 
        {
            $objForm.Close()
        }
    })
$objForm.MinimizeBox=$false
$objForm.MaximizeBox=$false
$objForm.ShowIcon=$false
$objForm.ShowInTaskbar=$false
$halfH=(($image.Width)/2)
$btnStart = New-Object System.Windows.Forms.Button
$btnStart.Location = New-Object System.Drawing.Size(1,$image.Height)
$btnStart.Size = New-Object System.Drawing.Size($halfH,25)
$btnStart.Text = "Start"
$btnStart.Add_Click({StartTimer; })
$objForm.Controls.Add($btnStart)
$btnStop = New-Object System.Windows.Forms.Button
$btnStop.Location = New-Object System.Drawing.Size($halfH,$image.Height)
$btnStop.Size = New-Object System.Drawing.Size($halfH,25)
$btnStop.Text = "Stop"
$btnStop.Add_Click({StopTimer; })
$objForm.Controls.Add($btnStop)
$btnStop.Enabled  = $true
$lblLog = New-Object System.Windows.Forms.Label
$lblLog.Location = New-Object System.Drawing.Size(10,($image.Height+25)) 
$lblLog.Size = New-Object System.Drawing.Size($image.Width,20) 
$lblLog.Text = "Refresh Info:"
$objForm.Controls.Add($lblLog)
$picb = New-Object Windows.Forms.PictureBox
$picb.size = New-Object Drawing.Size @($image.Width,$image.Height)
$picb.Location = New-Object Drawing.Point 1,1
$picb.Image = $image
$objForm.Controls.Add($picb)
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

除了使用带有静态图像的模型,我无法完全测试这一点,因为我没有android。
但这似乎对我有用:

我将这些行移到了脚本的顶部:

Set-ExecutionPolicy Bypass -Scope Process -Force
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms

$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000

function startTimer() { 
   $timer.Start()
   Write-Host "Timer Started"
}
function stopTimer() {
    $timer.Enabled = $false
    Write-Host "Timer Stopped"
    $btnStart.Text = "Continue"
}
function TimerTick()
{
    adb shell rm -f /sdcard/sc.png
    adb shell screencap -p /sdcard/sc.png
    adb pull /sdcard/sc.png $BpathTo

    $image = [System.Drawing.Image]::Fromfile("$BpathTo\sc.png")
    $picb.Image = $image
    $lblLog.Text = (Get-Date).ToString()
    Write-Host "Refreshed at: "(Get-Date).ToString()
    Write-Host ""
}
$timer.Add_Tick({TimerTick})
并在最后添加了以下内容:

# clean up
$timer.Stop()
$timer.Dispose()
$objForm.Dispose()
对我来说,这在模型中起到了作用,但我也看到了
$timer
的作用域是
$global:timer


希望这有帮助

除了使用带有静态图像的模型外,我无法完全测试这一点,因为我没有android。
但这似乎对我有用:

我将这些行移到了脚本的顶部:

Set-ExecutionPolicy Bypass -Scope Process -Force
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms

$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000

function startTimer() { 
   $timer.Start()
   Write-Host "Timer Started"
}
function stopTimer() {
    $timer.Enabled = $false
    Write-Host "Timer Stopped"
    $btnStart.Text = "Continue"
}
function TimerTick()
{
    adb shell rm -f /sdcard/sc.png
    adb shell screencap -p /sdcard/sc.png
    adb pull /sdcard/sc.png $BpathTo

    $image = [System.Drawing.Image]::Fromfile("$BpathTo\sc.png")
    $picb.Image = $image
    $lblLog.Text = (Get-Date).ToString()
    Write-Host "Refreshed at: "(Get-Date).ToString()
    Write-Host ""
}
$timer.Add_Tick({TimerTick})
并在最后添加了以下内容:

# clean up
$timer.Stop()
$timer.Dispose()
$objForm.Dispose()
对我来说,这在模型中起到了作用,但我也看到了
$timer
的作用域是
$global:timer


希望这能有所帮助

我认为您应该将函数向上移动一点(正好在
设置执行策略
的下方),因为现在您正在定义
TimerTick
函数之前添加它。另外,
[System.Reflection.Assembly]:LoadWithPartialName
已被弃用,请改用
添加类型-AssemblyName System.Windows.Forms
添加类型-AssemblyName System.Drawing
。完成后处理对象:
$timer.Dispose()
$objForm.Dispose()
@theo用于指针,并进行相应编辑。虽然你知道为什么照片没有更新吗?只是好奇。。我的答案对你有用吗?它仍然没有更新,使用临时工。解决方案是拍摄100个屏幕截图,然后进行覆盖,但这非常麻烦。我认为您应该将函数向上移动一点(就在
Set ExecutionPolicy
下面),因为现在您正在定义
TimerTick
函数之前添加它。另外,
[System.Reflection.Assembly]:LoadWithPartialName
已被弃用,请改用
添加类型-AssemblyName System.Windows.Forms
添加类型-AssemblyName System.Drawing
。完成后处理对象:
$timer.Dispose()
$objForm.Dispose()
@theo用于指针,并进行相应编辑。虽然你知道为什么照片没有更新吗?只是好奇。。我的答案对你有用吗?它仍然没有更新,使用临时工。解决方案是拍摄100个截图,然后覆盖,但它非常混乱