I have a Powershell script that displays a System.Windows.Forms
to trim audio files. This form loads an audio waveform that was created by the script.
When I close the form, I would like to remove this waveform file.
I have tried adding this but it doesn't work.
$form.Controls.Remove($pictureBox)
$form.Refresh()
Remove-Item -Path $FilepathForWaveform -Force
This also doesn't work:
$form.Dispose()
Remove-Item -Path $FilepathForWaveform -Force
I have seen this answer, but when I implement it, the script close without removing the image.
I use Powershell 5.1
My whole script is here and this is the relevant parts:
ffmpeg -i $file_name_complete -filter_complex "showwavespic=s=640x120" -frames:v 1 $DirectoryPath\"output.png" -y
$FilepathForWaveform = dir -LiteralPath $DirectoryPath\"output.png"
Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
$img = [System.Drawing.Image]::Fromfile($FilepathForWaveform)
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Width = $img.Size.Width
$pictureBox.Height = $img.Size.Height
$pictureBox.Image = $img
$pictureBox.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom
$pictureBox.Location = "15, 200"
$form.controls.add($pictureBox)
$form.ShowDialog()
Now, I am able to remove the image when the form is closed but the image is not displayed:
$FilepathForWaveform = dir -LiteralPath $DirectoryPath\"output.png"
$img = Image.FromStream(new MemoryStream(File.ReadAllBytes("[$FilepathForWaveform]")))
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Image = $img
$form.controls.add($pictureBox)
$form.Add_Closing({
$form.Controls.Remove($pictureBox)
$pictureBox.Dispose()
$form.Refresh()
Remove-Item -Path $FilepathForWaveform -Force
})
$form.ShowDialog() | Out-Null
$form.Dispose()
I have also tried this based on this link:
$img=[System.Drawing.Image]::FromStream([System.IO.MemoryStream]$FilepathForWaveform)