0

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)
MagTun
  • 5,619
  • 5
  • 63
  • 104
  • Dispose of the `PictureBox`. Removing it from the Controls collection does nothing to free the File stream. Disposing of the Control also removes it from the Controls collection (i.e., you just need `.Dispose()`). – Jimi Mar 28 '20 at 18:09
  • Thanks @Jimi, I try with `$form.Dispose()` (see my edit in the question) but it didn't work. – MagTun Mar 28 '20 at 20:14
  • 1
    Not the Form, the PictureBox. You can also use `Image.FromStream(new MemoryStream(File.ReadAllBytes("[File Path]")))` to load the Bitmap, so you don't lock the Stream. – Jimi Mar 28 '20 at 20:15
  • Thanks for you help @Jimi. I have tried several combinaison but without success. I have edited my question, could you please have a look at the bottom part (below the line) – MagTun Mar 29 '20 at 16:18
  • 1
    In PowerShell syntax this would be `$img = [System.Drawing.Image]::FromStream([System.IO.MemoryStream]::new([System.IO.File]::ReadAllBytes($FilePathForWaveform)))` – Theo Mar 30 '20 at 10:17
  • @Theo, thanks a lot, it's working and I don't even have to `.Dispose()` the `picturebox`. Please write an answer (you too @Jimi, so I can upvote you) – MagTun Mar 30 '20 at 16:20

0 Answers0