【发布时间】:2021-03-21 14:33:44
【问题描述】:
我正在尝试在 Visual Basic 中进行以下活动:
- 截取我的桌面截图并保存在图片框中。
- 保存在我的文件夹中
- 向某人发送附有图片的邮件
- 重命名图像并将其从文件夹中删除
这是每隔一小时重复一次的活动。到目前为止,我已经对上述所有步骤进行了编码,但是当脚本尝试重命名文件夹中的文件时,我收到一条错误消息。
代码如下:
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
我需要删除当前图像,然后保存一份新的副本。请帮忙。
【问题讨论】:
-
函数应该有一个返回类型和一个返回语句。
-
Using...End Using块应该用于具有Dispose方法的对象。
标签: vb.net image visual-studio picturebox