Imports System.Drawing.Imaging
Public Class Form1
Private WC As WebCamLibrary.WebCam
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
'Initialize web cam
WC = WebCamLibrary.WebCam.NewWebCam
Catch ex As Exception
'There is a problem, or the user doesnt have a web cam
MsgBox(ex.Message)
Application.Exit()
End Try
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'Set background image to a frame from the camera.
Me.BackgroundImage = Image.FromStream(New IO.MemoryStream(WC.GrabFrame))
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
'Set the interval to 100, which is 10 frames per second
Timer1.Interval = 1
Timer1.Start()
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
'Stop the timer
Timer1.Stop()
End Sub
Private Sub btnSnapShot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSnapShot.Click
Dim myBitmap As Bitmap
Dim myImageCodecInfo As ImageCodecInfo
Dim myEncoder As Encoder
Dim myEncoderParameter As EncoderParameter
Dim myEncoderParameters As EncoderParameters
' Create an image from the web cam.
myBitmap = New Bitmap(Image.FromStream(New IO.MemoryStream(WC.GrabFrame)))
' Get an ImageCodecInfo object that represents the JPEG codec.
myImageCodecInfo = GetEncoderInfo("image/jpeg")
' Create an Encoder object based on the GUID
' for the Quality parameter category.
myEncoder = Encoder.Quality
' Create an EncoderParameters object.
' An EncoderParameters object has an array of EncoderParameter
' objects. In this case, there is only one
' EncoderParameter object in the array.
myEncoderParameters = New EncoderParameters(1)
' Save the bitmap as a JPEG file with quality level 25. (you may set the quality between 0 - 100)
myEncoderParameter = New EncoderParameter(myEncoder, 25)
myEncoderParameters.Param(0) = myEncoderParameter
myBitmap.Save("theJpeg.jpg", myImageCodecInfo, myEncoderParameters)
End Sub
Private Shared Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
Dim i As Integer
Dim encoders() As ImageCodecInfo
'Get all image encoders
encoders = ImageCodecInfo.GetImageEncoders()
'loop through encoders and return the matching encoder
i = 0
While i < encoders.Length
If encoders(i).MimeType = mimeType Then
Return encoders(i)
End If
i += 1
End While
Return Nothing
End Function
End Class