View Full Version : Saving JPEG images (sample code here)

Stefan Geissler
October 7, 2003, 08:18:57
An image can be saved as JPEG image, if the JPEG.DLL is used. First of all, two necessary functions must be declared:
The saving function:

Private Declare Function DIBtoJPEG Lib "jpeg.dll" _
(ByVal name As String, _
ByVal Quality As Integer, _
ByVal Dib As Long) As Long

The function to free memory:

Private Declare Function GlobalFree Lib "kernel32" _
(ByVal hMem As Long) As Long

The image saving uses a counter to create the image file names. A series of image can be snapped and saved.

Private Sub cmdSaveJPEG_Click()
Dim FileName As String
Dim DIBPointer As Long ' Pointer to the DIB

' 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 VideoHasStopped = False Then
' The live video is running. Therefore, we need to grab the
' current frame.
End If
' Build the file name of the jpeg image
ImageNumber = ImageNumber + 1
FileName = "image" + LTrim(Str(ImageNumber)) + ".jpg"

' Retrieve the pointer to the DIB
DIBPointer = ICImagingControl1.MemoryGetDib
' Save the DIB as JPEG image
DIBtoJPEG FileName, 80, DIBPointer
'Free the memory of the DIB. This is VERY important.
GlobalFree DIBPointer
MsgBox "Please click the Start Live button first!"
End If

Exit Sub

End Sub

The jpeg.dll must reside in the Windows/System32 or in the current working directory of the application. A complete Visual Basic project including the jpeg.dll is attached to this post. It is only possible to save 24 bit images (See MemoryCurrentGrabberColorFormat property of IC Imaging Control)