0

I'm trying to do the following activity in visual basic:

  • Take a screenshot of my desktop and hold it in picture box.
  • Save it on my folder
  • Send a mail to someone with the image attached
  • Rename the image and Delete it from the folder

This is a recurring activity for every one hour. So far I have coded for all the steps above but when the script tries to rename the file on the folder I get an error message.

Error code

The code goes something like this:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim theTime As DateTime
    theTime = Now.ToLongTimeString
    Dim regDate As Date = DateTime.Now()
    strDate = regDate.ToString("ddMMMyyyy HH:mm:ss")
    'MsgBox(theTime)
    'currentfolder = "C:\Users\user\Desktop" + strDate
    'My.Computer.FileSystem.CreateDirectory(currentfolder)
    If theTime >= #8:00:00 AM# Then
        Timer1.Interval = 100
        Timer1.Start()
        'Timer starts functioning
    End If
End Sub


Function Takescreenshot()
    Dim bounds As Rectangle
    Dim screenshot As System.Drawing.Bitmap
    Dim graph As Graphics
    Dim FileToDelete As String
    FileToDelete = "C:\Users\user\Desktop\1.png"

    If System.IO.File.Exists(FileToDelete) = True Then
        PictureBox1.Image = Nothing
        My.Computer.FileSystem.RenameFile("C:\Users\user\Desktop\1.png", "2.png")
        System.IO.File.Delete("C:\Users\user\Desktop\2.png")
    End If

    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



    SaveImage("C:\Users\user\Desktop\1.png", PictureBox1.Image)

    'Send Email
    Second = 0
    Timer1.Start()
End Function

Public Sub SaveImage(filename As String, image As Image)
    Dim path As String = System.IO.Path.Combine(My.Application.Info.DirectoryPath, filename & ".png")
    Dim mySource As New Bitmap(image.Width, image.Height)
    Dim grfx As Graphics = Graphics.FromImage(mySource)
    grfx.DrawImageUnscaled(image, Point.Empty)
    grfx.Dispose()
    mySource.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg)
    mySource.Dispose()
End Sub

I need to delete the current image and then save a fresh copy of it. Please help.

Mary
  • 14,926
  • 3
  • 18
  • 27
k r harsha
  • 15
  • 5

1 Answers1

0

Ok I found out a solution for this problem. I actually found out what was the process that was really holding the script from deleting the image. It was the mail function which I have not added for security reasons. I had to dispose the image that was being added to the email body after the work was done so

img1.Dispose()

solved the issue

k r harsha
  • 15
  • 5
  • Since your problem has been solved, so please consider [accepting it](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). – Xingyu Zhao Dec 17 '20 at 06:09