Visual Basic Express 2008 - Working With USB Cameras and JPG Images

Part II

Download VB Express Project Files


This VB Express Project has the capability to save and replay jpg images captured from the selected video source, typically a USB camera.  The window on the left contains live video from the USB camera and the window on the right displays the image captured when the Capture button is clicked.  Clicking the Save button stores the image to a folder with the current Date as a name.  The image name contains the date and time in the format YYYYMMDDmmmss.  Images can be retrieved and displayed in the capture window using the Playback controls.

   
Return to:  
Motion Patrol
Basic Video Capture
Next - Part III - Detect Motion and Draw on Jpeg Image
   
   
   

 

 

Selections of VB Code (Download the project file for full program code)

Private Sub Capture_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Capture_Button.Click

    SendMessage(hWnd, WM_CAP_GRAB_FRAME, 0, 0) 'Grab the frame

    SendMessage(hWnd, WM_CAP_EDIT_COPY, 0, 0) 'Copy it to the clip board

    SendMessage(hWnd, WM_CAP_SET_PREVIEW, True, 0) 'Keep previewing in live window

   

    'Copy clipboard to a bitmap image (we will use there later to manipulate image)

    BitmapCaptureImageA = CType(Clipboard.GetDataObject().GetData(GetType(System.Drawing.Bitmap)), Image)

  

     ' Send Bitmap image to the picturebox

    PictureBox1.Image = BitmapCaptureImageA.GetThumbnailImage(320, 240, Nothing, Nothing)

End Sub

 

 

 

'save the image in loaction C:\USBCamera\Images\CurrentDate\DateAndTime.jpg

Private Sub SaveImage()

Dim dirname As String = Format(Now(), "yyyyMMdd") 'The directory name for images will be the current date

Dim stringdate As String = Format(Now(), "yyyyMMddHHmmss") 'The image name will be the date and time

Dim BitmapFileName As String 'name of the file to be saved

Dim fileList As System.Collections.ObjectModel.ReadOnlyCollection(Of String) 'this will hold the names of the files in the image directory

'Create a directory for the images if it does not exist

If My.Computer.FileSystem.DirectoryExists(DataDirectory & dirname) Then

'Do Nothing (space holder for message box

Else

Try

My.Computer.FileSystem.CreateDirectory(DataDirectory & dirname)

Catch

MsgBox("Error Creating Directory " & ErrorToString())

End Try

End If

' Save image

Try

fileList = My.Computer.FileSystem.GetFiles(DataDirectory & dirname & "\", FileIO.SearchOption.SearchTopLevelOnly, "*.jpg")

BitmapFileName = DataDirectory & dirname & "\" & stringdate & ReplayFileCount & ".jpg"

BitmapCaptureImageA.Save(BitmapFileName, System.Drawing.Imaging.ImageFormat.Jpeg)

'Update Textboxes

ImageNameTextBox.Text = Strings.Right(fileList(ReplayFileCount), 19)

ImageNumberTextBox.Text = fileList.Count + 1

TotalImagesTextBox.Text = fileList.Count + 1

DirectoryTextBox.Text = DataDirectory & dirname

ReplayFileCount = fileList.Count

Catch

MsgBox("Error Saving Image " & ErrorToString())

End Try

End Sub

 

 

'Open a Folder Dialog to Select the Image Folder for replay

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenDirButon.Click

Dim fileList As System.Collections.ObjectModel.ReadOnlyCollection(Of String) 'this will hold the names of the files in the image directory

Dim dlgFolder As System.Windows.Forms.FolderBrowserDialog 'Open directory dialog

Dim dirname As String

ReplayFileCount = 0 ' Rest playback file counter to 1st image

Try

Dim sString() As String = System.IO.Directory.GetDirectories(DataDirectory, "*.", SearchOption.TopDirectoryOnly) 'sets starting point for folder search

dlgFolder = New System.Windows.Forms.FolderBrowserDialog 'start open folder dialog

dlgFolder.ShowNewFolderButton = False 'hide the New Folder option

dlgFolder.SelectedPath = sString(0)

dlgFolder.Description = "Select the folder that contains Image files." 'diplays dialog messag

If dlgFolder.ShowDialog() = DialogResult.OK Then

DirectoryTextBox.Text = dlgFolder.SelectedPath 'display folder name in text box

dirname = dlgFolder.SelectedPath 'holds the name of the selected folder

fileList = My.Computer.FileSystem.GetFiles(dirname & "\", FileIO.SearchOption.SearchTopLevelOnly, "*.jpg") 'holds the names of image files from selected folder

PictureBox1.Image = Image.FromFile(fileList(0)).GetThumbnailImage(320, 240, Nothing, Nothing) 'display the first image in the folder

'Update textboxes

ImageNameTextBox.Text = Strings.Right(fileList(ReplayFileCount), 19)

ImageNumberTextBox.Text = ReplayFileCount + 1

TotalImagesTextBox.Text = fileList.Count

            End If

Catch

         MsgBox("Error Opening Image Directory, There May Not Be Any Images Saved, Try Saving An Image First......... " & ErrorToString())

End Try

End Sub

 

'Advance the replay image ahead 1 file

Private Sub NextImageButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NextImageButton.Click

Dim fileList As System.Collections.ObjectModel.ReadOnlyCollection(Of String) 'this will hold the names of the files in the image directory

Dim dirname As String

Try

dirname = DirectoryTextBox.Text

fileList = My.Computer.FileSystem.GetFiles(dirname & "\", FileIO.SearchOption.SearchTopLevelOnly, "*.jpg") 'holds the names of image files from selected folder

If ReplayFileCount < fileList.Count - 1 Then

    ReplayFileCount = ReplayFileCount + 1

End If

ImageNameTextBox.Text = Strings.Right(fileList(ReplayFileCount), 19)

ImageNumberTextBox.Text = ReplayFileCount + 1

TotalImagesTextBox.Text = fileList.Count

PictureBox1.Image = Image.FromFile(fileList(ReplayFileCount)).GetThumbnailImage(320, 240, Nothing, Nothing) 'display the first image in the folder

Catch ex As Exception

End Try

End Sub

 

 

'Move one image backwards

Private Sub PrevImageButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrevImageButton.Click

Dim fileList As System.Collections.ObjectModel.ReadOnlyCollection(Of String) 'this will hold the names of the files in the image directory

Dim dirname As String

Try

If ReplayFileCount > 0 Then

    ReplayFileCount = ReplayFileCount - 1

End If

dirname = DirectoryTextBox.Text

fileList = My.Computer.FileSystem.GetFiles(dirname & "\", FileIO.SearchOption.SearchTopLevelOnly, "*.jpg") 'holds the names of image files from selected folder

'Update Textboxes

ImageNameTextBox.Text = Strings.Right(fileList(ReplayFileCount), 19)

ImageNumberTextBox.Text = ReplayFileCount + 1 'Because the filelist starts at 0, I add one for display purposes

TotalImagesTextBox.Text = fileList.Count

PictureBox1.Image = Image.FromFile(fileList(ReplayFileCount)).GetThumbnailImage(320, 240, Nothing, Nothing) 'display the first image in the folder

 

Catch ex As Exception

 

End Try

End Sub