I’m working on a security camera application using the webcam that takes a photo every x amount of ms the user chooses. (Default at 2000ms, or 2s) Currently the files are stored on the user’s hard drive by default.
What I’m trying to do is instead of saving it as 1.bmp, 2.bmp, 3.bmp, etc: but to the date and time, ex. “Jul 03 12:14:53.bmp” using this – DateTime.Now.ToString(“MMM dd hh:mm:ss”)
However obviously I’m having no luck, and continue to get errors. So here’s my code before I got all these errors, and any help would be greatly appreciated. Thanks!
Dim frame As Integer
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
frame += 1
Dim r As RECT = New RECT()
GetWindowRect(hWnd, r)
Dim cWidth As Integer = r.right - r.left
Dim cHeight As Integer = r.bottom - r.top
Dim bmp As Bitmap = New Bitmap(cWidth, cHeight)
Dim gfx As Graphics = Graphics.FromImage(bmp)
Dim gHdc As IntPtr = gfx.GetHdc()
'refresh the image
SendMessage(hWnd, WM_CAP_GRAB_FRAME, IntPtr.Zero, IntPtr.Zero)
PrintWindow(hWnd, gHdc, 0)
gfx.ReleaseHdc(gHdc)
gfx.Dispose()
bmp.Save("photos-taken\" & frame & ".bmp")
bmp.Dispose()
End Sub
Private Sub RecordToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RecordToolStripMenuItem.Click
If Not Directory.Exists("photos-taken") Then
Directory.CreateDirectory("photos-taken")
End If
If RecordToolStripMenuItem.Checked = False Then
RecordToolStripMenuItem.Checked = True
frame = 0
Timer2.Interval = 2000
Timer2.Start()
Else
RecordToolStripMenuItem.Checked = False
frame = 0
Timer2.Stop()
End If
End Sub
You could substitute any date format that’s a valid file name (i.e. no
/,\or:), also using yyyyMMddHHmmss you get your files sorted by name in order they are saved.