-1

I'm using a timer to take screen captures after an amount of time and save the images to a specific path.

Private Sub tmrPS1_Tick(sender As Object, e As EventArgs) Handles tmrPS1.Tick
    Dim bounds As Rectangle
    Dim screenshot As System.Drawing.Bitmap
    Dim graph As Graphics
    bounds = Screen.PrimaryScreen.Bounds
    screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
    graph = Graphics.FromImage(screenshot)
    graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
    PictureBox1.Image = screenshot
    PictureBox1.Image.Save("C:\ImagesFolder\1.jpg")
    tmrPS1.Enabled = False
End Sub

And I want another timer to delete them after I sent them with mail because I will have to take new ones. My question is how do I delete the images knowing the path?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Victor
  • 109
  • 1
  • 14
  • 1
    System.IO.File.Delete(path)? https://msdn.microsoft.com/en-us/library/system.io.file.delete%28v=vs.110%29.aspx – TyCobb Feb 02 '15 at 19:19
  • If you save the file always with the same name what is the necessity to delete it everytime you save it? – Steve Feb 02 '15 at 19:34
  • I take a couple of print screens in a minute save them and send them through email. After that the cicle starts all over again every minute. Or it should. The problem is that after the first email when the new images need to be taken (ex: the old 1.jpg must be replaced with the new 1.jpg) i get an error that says ("A generic error occurred in GDI+.") on this line of code: `PictureBox1.Image.Save("C:\xampp\debug\1.jpg")` – Victor Feb 02 '15 at 19:40
  • 1
    Ah this is a well know pitfall with PictureBox. See here http://stackoverflow.com/a/14866755/1197518 – Steve Feb 02 '15 at 20:23
  • It seems like if I restart the aplication every minute instead of just reseting the timer I can get it to work without any errors. I think I'll let it as it is for now. Thanks for help. – Victor Feb 02 '15 at 20:34
  • you do not need a UI control like PictureBox in order to save the file/image; that code is also leaking resources – Ňɏssa Pøngjǣrdenlarp Feb 02 '15 at 21:22

1 Answers1

1

Delete/recreate the folder when you are done with it?

        If IO.Directory.Exists(DestinationFolder) Then IO.Directory.Delete(DestinationFolder, True)
        Application.DoEvents()
        IO.Directory.CreateDirectory(DestinationFolder)

This code cleans up files in "Temp" with the same extension as the file I'm saving.

                With My.Computer.FileSystem
                    Dim s As String = Environ("temp")
                    For Each foundFile As String In .GetFiles(s, FileIO.SearchOption.SearchTopLevelOnly, "*.tmp.kml")
                        .DeleteFile(foundFile) ' clean up old output
                    Next
               End With
rheitzman
  • 2,247
  • 3
  • 20
  • 36