This chapter shows you how to grab a still image from the video stream and save it to a .png file.
The source code for this sample program can be found in the samples\VB6\Grabbing an Image directory.
Create a new project and add IC Imaging Control to the form. Before you run the program, select the video device, input and video format as shown in the First Steps in Visual Basic 6 chapter. Alternatively, run the program without selecting a device. In this case, the program shows the device selection dialog provided by IC Imaging Control. If you close this dialog without having first make a selection, the program displays an error message and terminates.
Now add 3 buttons to the form and label them Start Live, Stop Live and Save Bitmap. Name the buttons cmdStartLive, cmdStopLive and cmdSaveBitmap respectively.
Private Sub cmdStartLive_Click() ICImagingControl1.LiveStart ' Store in VideoHasStarted whether a video stream ' has been started once. If VideoHasStarted is True, ' you can be sure that there is an image that can be saved. VideoHasStarted = True End Sub
Private Sub cmdStopLive_Click() ICImagingControl1.LiveStop End Sub
Run the program. You can now start and stop the live image by clicking on the respective buttons.
The video format determines the video stream's image size and color depth. Select one in the VideoFormat box:
When the live video is running, a call to the method MemorySnapImage Method will grab the currently displayed image and stores it in IC Imaging Control's image buffer. On the other hand, when you stop the live image, the last frame of the live image is stored automatically in IC Imaging Control's image buffer for further display. In both cases, we can save the currently displayed image by calling the MemorySaveImage method. We only need to add a little error handling and a common dialog box so that we can select a filename. Drag a Common Dialog Box to your form and put this code in the Save Bitmap button's Click event:
Private Sub cmdSaveBitmap_Click() On Error GoTo cmdSaveBitmap_Click ' Check whether the video stream has been started once. ' If it has been started once, there are two possible situations: ' 1) The live video is running. In this case we grab the current frame ' in order to save it. ' 2) The live video has been stopped. In this case the last frame of the ' live video was grabbed automatically and we can save it now. If VideoHasStarted = True Then If ICImagingControl1.LiveVideoRunning Then ' The live video is running. Therefore, we need to grab the ' current frame. ICImagingControl1.MemorySnapImage End If With CommonDialog1 .Filter = "Bitmap (*.png)|*.png|" .CancelError = True .ShowSave ICImagingControl1.MemorySaveImage .FileName End With Else MsgBox "Please click the Start Live button first!" End If Exit Sub cmdSaveBitmap_Click: If Err <> cdlCancel Then MsgBox Err.Description End Sub
Now you can start the live image to adjust your cameras brightness and focus, and then stop the live display and save the captured image.
Established in 1990, The Imaging Source is one of the leading manufacturers of industrial cameras, frame grabbers and video converters for production automation, quality assurance, logistics, medicine, science and security.
Our comprehensive range of cameras with USB 3.1, USB 3.0, USB 2.0, GigE interfaces and other innovative machine vision products are renowned for their high quality and ability to meet the performance requirements of demanding applications.