|
Visual Basic Express 2008 - Working With USB Cameras and JPG Images Part II |
|
|
|
|
|
Download VB Express Project Files |
|
| 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.ClickSendMessage(hWnd, WM_CAP_GRAB_FRAME, 0, 0)
'Grab the frameSendMessage(hWnd, WM_CAP_EDIT_COPY, 0, 0)
'Copy it to the clip boardSendMessage(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 imageTry
My.Computer.FileSystem.GetFiles(DataDirectory & dirname & "\", FileIO.SearchOption.SearchTopLevelOnly, "*.jpg")fileList =
BitmapFileName = DataDirectory & dirname &
"\" & stringdate & ReplayFileCount & ".jpg"BitmapCaptureImageA.Save(BitmapFileName, System.Drawing.Imaging.ImageFormat.Jpeg)
'Update TextboxesImageNameTextBox.Text = Strings.Right(fileList(ReplayFileCount), 19)
ImageNumberTextBox.Text = fileList.Count + 1
TotalImagesTextBox.Text = fileList.Count + 1
DirectoryTextBox.Text = DataDirectory & dirname
ReplayFileCount = fileList.Count
Catch"Error Saving Image " & ErrorToString()) End Try End SubMsgBox(
'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 imageTry
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 dialogdlgFolder.ShowNewFolderButton =
False 'hide the New Folder optiondlgFolder.SelectedPath = sString(0)
dlgFolder.Description =
"Select the folder that contains Image files." 'diplays dialog messagIf dlgFolder.ShowDialog() = DialogResult.OK Then
'display folder name in text boxDirectoryTextBox.Text = dlgFolder.SelectedPath
dirname = dlgFolder.SelectedPath
'holds the name of the selected folderfileList =
My.Computer.FileSystem.GetFiles(dirname & "\", FileIO.SearchOption.SearchTopLevelOnly, "*.jpg") 'holds the names of image files from selected folderPictureBox1.Image = Image.FromFile(fileList(0)).GetThumbnailImage(320, 240,
Nothing, Nothing) 'display the first image in the folder 'Update textboxesImageNameTextBox.Text = Strings.Right(fileList(ReplayFileCount), 19)
ImageNumberTextBox.Text = ReplayFileCount + 1
TotalImagesTextBox.Text = fileList.Count
End If
CatchMsgBox(
"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
My.Computer.FileSystem.GetFiles(dirname & "\", FileIO.SearchOption.SearchTopLevelOnly, "*.jpg") 'holds the names of image files from selected folderdirname = DirectoryTextBox.Text
fileList =
If ReplayFileCount < fileList.Count - 1 Then
ReplayFileCount = ReplayFileCount + 1
End IfImageNameTextBox.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 Ifdirname = DirectoryTextBox.Text
fileList =
My.Computer.FileSystem.GetFiles(dirname & "\", FileIO.SearchOption.SearchTopLevelOnly, "*.jpg") 'holds the names of image files from selected folder 'Update TextboxesImageNameTextBox.Text = Strings.Right(fileList(ReplayFileCount), 19)
ImageNumberTextBox.Text = ReplayFileCount + 1
'Because the filelist starts at 0, I add one for display purposesTotalImagesTextBox.Text = fileList.Count
PictureBox1.Image = Image.FromFile(fileList(ReplayFileCount)).GetThumbnailImage(320, 240,
Nothing, Nothing) 'display the first image in the folderCatch ex As Exception
End Try
End Sub