Header Ads Widget

Ticker

6/recent/ticker-posts

WebCamp Capture with VB.Net

Banyak yang bertanya, bagaimana mengambil gambar dari Webcamp dengan VB.Net ??
Nah kali ini saya akan share caranya, caranya cukup mudah, di mbah google juga sudah banyak, tp kadang msh banyak yang kurang sesuai, seperti pas preview/start camera tidak muncul gambar kita, jd kita tidak bisa lihat gambar atau posisi kita seperti apa nanti pas di capture..

nah begini caranya :
  • Design Form Seperti dibawah ini (Saya anggap anda sudah memahami cara design form)

Tool yang diperlukan adalah:
1. 2 buah picture box
2. 7 buah button

PictureBox1 = imgVideo
PictureBox2 = imgCapture
Button1 = bntStart
Button2 = bntStop
Button3 = bntContinue
Button4 = bntCapture
Button5 = bntSave
Button6 = bntVideoFormat
Button7 = bntVideoSource
  • Setelah selesai Design Form, Lanjut ke Coding, Lets Go !!!
Pertama anda harus ADD REFERENCE (.dll) dulu agar program berjalan lancar, saya anggap anda sudah tau cara ADD REFERENCE.
  • Setelah Add Reference, tambahkan 2 buah Class diproject anda, class1 kasih nama Helper.vb dan class2 kasih nama WebCam.vb
  • Coding class Helper.vb
Imports System
Imports System.IO
Imports System.Linq
Imports System.Text
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Windows.Forms

'Design by Pongsakorn Poosankam
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
  • Coding class WebCam.vb
Imports System
Imports System.IO
Imports System.Linq
Imports System.Text
Imports WebCam_Capture
Imports System.Collections.Generic

    'Design by Pongsakorn Poosankam
    Class WebCam
        Private webcam As WebCamCapture
        Private _FrameImage As System.Windows.Forms.PictureBox
        Private FrameNumber As Integer = 30
        Public Sub InitializeWebCam(ByRef ImageControl As System.Windows.Forms.PictureBox)
            webcam = New WebCamCapture()
            webcam.FrameNumber = CULng((0))
            webcam.TimeToCapture_milliseconds = FrameNumber
            AddHandler webcam.ImageCaptured, AddressOf webcam_ImageCaptured
            _FrameImage = ImageControl
        End Sub

        Private Sub webcam_ImageCaptured(ByVal source As Object, ByVal 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
  • Double Click Form1 anda, nama form 1 saya kasih nama mainWinForm.
Private Sub mainWinForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        webcam = New WebCam()
        webcam.InitializeWebCam(imgVideo)
    End Sub
  • Coding bntStart
 Private Sub bntStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntStart.Click
        webcam.Start()
    End Sub
  • Coding bntStop
Private Sub bntStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntStop.Click
        webcam.Stop()
    End Sub
  • Coding bntContinue
 Private Sub bntContinue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntContinue.Click
        webcam.Continue()
    End Sub
  • Coding bntCapture
Private Sub bntCapture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntCapture.Click
        imgCapture.Image = imgVideo.Image
    End Sub
  • Coding bntSave
Private Sub bntSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntSave.Click
        Helper.SaveImageCapture(imgCapture.Image)
    End Sub
  • Coding bntVodeoFormat
 Private Sub bntVideoFormat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntVideoFormat.Click
        webcam.ResolutionSetting()
    End Sub
  • bntVodeoSource
Private Sub bntVideoSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntVideoSource.Click
        webcam.AdvanceSetting()
    End Sub
  • Lalu RUN project, Selesai . .
Bila anda masih bingung, tenang saya akan kasih project jadinya jadi jangan kwatir.. hehehe ^_^

Oke Langsung saya anda bisa download projectnya dibawah ini :

Download DLL nya Disini Aja
Download Source Code Project Disini Aja

Semoga Bermanfaat :) 

Post a Comment

0 Comments