我是新的Powershell。我试图创建图片框,将加载大量的图像在一个文件夹。我有一个文件夹,包含10个图像。但我希望我的图片框显示它动态每秒钟或3秒
以下是目前为止我代码。
####################################### Form settings ##############################################
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$Form.Anchor = "Top,Bottom,Left,Right"
$Form.Size = New-Object System.Drawing.Size(1920,1600)
$Form.AutoScale = $True
$Form.StartPosition = "CenterScreen" #loads the window in the center of the screen
$Form.BackgroundImageLayout = "Zoom"
$Form.MinimizeBox = $True
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Auto"
$Form.AutoSizeMode = New-Object System.Windows.Forms.AutoSizeMode
$Form.SizeGripStyle = "Show"
$Form.BackColor = "LightGray"
###################################################################################################
###################################################################################################
######################################### Image Folder ############################################
$ImagePreview = New-Object System.Windows.Forms.PictureBox
$ImagePreview.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::StretchImage
$ImagePreview.Location = New-Object System.Drawing.Size(965,110)
$ImagePreview.Size = New-Object System.Drawing.Size(295,370)
$ImagePreview.BackColor = "White"
$Form.Controls.Add($ImagePreview)
Function ImageFolder($ImagePreview)
{
$ImageItem = Get-Item "C:\Newfolder"
$ImagePreview.ImageLocation = $ImageItem
}
$TimerImageFolder = New-Object System.Windows.Forms.Timer
$TimerImageFolder.Interval = 3000
$TimerImageFolder.Add_Tick({$ImageFolder $ImagePreview})
$TimerImageFolder.Enabled = $True
$ImageGroupBox = New-Object System.Windows.Forms.GroupBox
$ImageGroupBox.Location = New-Object System.Drawing.Size(940,70)
$ImageGroupBox.size = New-Object System.Drawing.Size(350,440)
$ImageGroupBox.text = "Preview"
$ImageGroupBox.BackColor = "DimGray"
$ImageGroupBox.ForeColor = "White"
$Form.Controls.Add($ImageGroupBox)
####################################### Result ##############################################
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
我不知道哪部分错了。非常感谢你们的帮助
1条答案
按热度按时间k75qkfdt1#
您尝试从PowerShell执行的是一项繁琐的任务。您可能注意到,它需要示例化
powershell
instance,其中初始化Forms.Timer
,这是必需的,因为我们需要在单独的线程上执行.Tick
event,否则,每次触发此事件时表单都会冻结。下面共享的代码是,作为您试图实现的最小复制,我已经尝试尽可能地简化,但如前所述,这不是一个简单的任务从PowerShell。
下面的例子以及另一个例子使用一个API从互联网而不是从磁盘加载图片可以在this Gist上找到。那里还有一个gif演示。