VB.NET Free Source Code Capture webcam images and save them to your computer


VB.NET Free Source Code: Capture webcam images and save them to your computer

Note: You need to download and install the WebCam_Capture.dll file.


Visual Studio 2019 ถ่ายรูปจากกล้อง Webcam หรือกล้องมือถือ แล้วเซฟไฟล์ลงบนคอม.


Module: WebCam.vb


Imports WebCam_Capture


Class WebCam

    Private webcam As WebCamCapture

    Private _FrameImage As PictureBox

    Private FrameNumber As Integer = 30

    Public Sub InitializeWebCam(ByRef ImageControl As PictureBox)

        webcam = New WebCamCapture()

        webcam.FrameNumber = 0

        webcam.TimeToCapture_milliseconds = FrameNumber

        AddHandler webcam.ImageCaptured, AddressOf webcam_ImageCaptured

        _FrameImage = ImageControl

    End Sub


    Private Sub webcam_ImageCaptured(source As Object, e As WebcamEventArgs)

        _FrameImage.Image = e.WebCamImage

    End Sub


    Public Sub Start()

        webcam.TimeToCapture_milliseconds = FrameNumber

        webcam.Start(0)

    End Sub


    Public Sub [Stop]()

        webcam.[Stop]()

    End Sub


    Public Sub [Continue]()

        ' change the capture time frame

        webcam.TimeToCapture_milliseconds = FrameNumber


        ' resume the video capture from the stop

        webcam.Start(Me.webcam.FrameNumber)

    End Sub


    Public Sub ResolutionSetting()

        webcam.Config()

    End Sub


    Public Sub AdvanceSetting()

        webcam.Config2()

    End Sub


End Class


Module: WebCam.vb


Imports System.IO


Class Helper


    Public Shared Sub SaveImageCapture(ByVal image As System.Drawing.Image)


        Dim s As New SaveFileDialog()

        s.FileName = "Image"

        ' Default file name

        s.DefaultExt = ".Jpg"

        ' Default file extension

        s.Filter = "Image (.jpg)|*.jpg"

        ' Filter files by extension

        ' Show save file dialog box

        ' Process save file dialog box results

        If s.ShowDialog() = DialogResult.OK Then

            ' Save Image

            Dim filename As String = s.FileName

            Dim fstream As New FileStream(filename, FileMode.Create)

            image.Save(fstream, System.Drawing.Imaging.ImageFormat.Jpeg)

            fstream.Close()

        End If

    End Sub


End Class


Form: Form1.vb


Option Explicit On

Option Strict On


Public Class Form1


    Private webcam As WebCam


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        webcam = New WebCam()

        webcam.InitializeWebCam(imgVideo)

    End Sub


    Private Sub bntStart_Click(sender As Object, e As EventArgs) Handles bntStart.Click

        webcam.Start()

    End Sub


    Private Sub bntStop_Click(sender As Object, e As EventArgs) Handles bntStop.Click

        webcam.Stop()

        imgVideo.Image = Nothing

    End Sub


    Private Sub bntContinue_Click(sender As Object, e As EventArgs) Handles bntContinue.Click

        webcam.Continue()

    End Sub


    Private Sub bntCapture_Click(sender As Object, e As EventArgs) Handles bntCapture.Click

        If Not IsNothing(imgVideo.Image) Then

            imgCapture.Image = imgVideo.Image

        Else

            imgCapture.Image = Nothing

        End If

    End Sub


    Private Sub bntSave_Click(sender As Object, e As EventArgs) Handles bntSave.Click

        If Not IsNothing(imgCapture.Image) Then

            Helper.SaveImageCapture(imgCapture.Image)

        End If

    End Sub


End Class


Comments

  1. Config is not a member of WebcamCapture
    Config is not a member of WebcamCapture

    Please help me to fix this.Thank You

    ReplyDelete

Post a Comment