Hi Faizal,
Check this example. Now please take its reference and correct your code.
First Download the dll from the below link and add its reference in your project.
http://www.mediafire.com/?dxrnc433pfrp7mu
Form Design
I have added two PictureBox controls in the Form and 7 Button controls.
![](https://i.imgur.com/OMXlm3T.jpg)
Class File
C#
using WebCam_Capture;
namespace _WebCam_PictureBox
{
class WebCam
{
private WebCamCapture webcam;
private System.Windows.Forms.PictureBox _FrameImage;
private int FrameNumber = 30;
public void InitializeWebCam(ref System.Windows.Forms.PictureBox ImageControl)
{
webcam = new WebCamCapture();
webcam.FrameNumber = ((ulong)(0ul));
webcam.TimeToCapture_milliseconds = FrameNumber;
webcam.ImageCaptured += new WebCamCapture.WebCamEventHandler(webcam_ImageCaptured);
_FrameImage = ImageControl;
}
void webcam_ImageCaptured(object source, WebcamEventArgs e)
{
_FrameImage.Image = e.WebCamImage;
}
public void Start()
{
webcam.TimeToCapture_milliseconds = FrameNumber;
webcam.Start(0);
}
public void Stop()
{
webcam.Stop();
}
public void Continue()
{
// change the capture time frame.
webcam.TimeToCapture_milliseconds = FrameNumber;
// resume the video capture from the stop.
webcam.Start(this.webcam.FrameNumber);
}
public void ResolutionSetting()
{
webcam.Config();
}
public void AdvanceSetting()
{
webcam.Config2();
}
}
}
VB.Net
Imports WebCam_Capture
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((0UL)))
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]()
webcam.TimeToCapture_milliseconds = FrameNumber
webcam.Start(Me.webcam.FrameNumber)
End Sub
Public Sub ResolutionSetting()
webcam.Config()
End Sub
Public Sub AdvanceSetting()
webcam.Config2()
End Sub
End Class
Code
C#
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace _WebCam_PictureBox
{
public partial class mainWinForm : Form
{
public mainWinForm()
{
InitializeComponent();
}
WebCam webcam;
private void mainWinForm_Load(object sender, EventArgs e)
{
webcam = new WebCam();
webcam.InitializeWebCam(ref imgVideo);
}
private void bntStart_Click(object sender, EventArgs e)
{
webcam.Start();
}
private void bntStop_Click(object sender, EventArgs e)
{
webcam.Stop();
}
private void bntContinue_Click(object sender, EventArgs e)
{
webcam.Continue();
}
private void bntCapture_Click(object sender, EventArgs e)
{
imgCapture.Image = imgVideo.Image;
}
private void bntSave_Click(object sender, EventArgs e)
{
SaveImageCapture(imgCapture.Image);
}
private void bntVideoFormat_Click(object sender, EventArgs e)
{
webcam.ResolutionSetting();
}
private void bntVideoSource_Click(object sender, EventArgs e)
{
webcam.AdvanceSetting();
}
public static void SaveImageCapture(System.Drawing.Image image)
{
SaveFileDialog s = new SaveFileDialog();
s.FileName = "Image";
s.DefaultExt = ".Jpg";
s.Filter = "Image (.jpg)|*.jpg";
// Save Image in Project Folder.
string filePath = Path.GetDirectoryName(Application.ExecutablePath) + @"\File\";
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
string filename = filePath + "Image.jpg";
FileStream fstream = new FileStream(filename, FileMode.Create);
image.Save(fstream, System.Drawing.Imaging.ImageFormat.Jpeg);
fstream.Close();
// Add waterMark Text.
using (Bitmap bmp = new Bitmap(filename, false))
{
using (Graphics grp = Graphics.FromImage(bmp))
{
//Set the Color of the Watermark text.
Brush brush = new SolidBrush(Color.White);
//Set the Font and its size.
Font font = new System.Drawing.Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel);
//Determine the size of the Watermark text.
SizeF textSize = new SizeF();
textSize = grp.MeasureString("@ASPSnippets.com", font);
//Position the text and draw it on the image.
Point position = new Point((bmp.Width - ((int)textSize.Width + 10)), (bmp.Height - ((int)textSize.Height + 10)));
grp.DrawString("@ASPSnippets.com", font, brush, position);
}
// Show save file dialog box.
if (s.ShowDialog() == DialogResult.OK)
{
// Save Image.
bmp.Save(s.FileName);
}
}
// Delete Directory.
if (Directory.Exists(filePath))
{
Directory.Delete(filePath, true);
}
}
}
}
VB.Net
Imports System.IO
Public Class Form1
Dim webcam As WebCam
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
webcam = New WebCam()
webcam.InitializeWebCam(imgVideo)
End Sub
Private Sub bntStart_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnStart.Click
webcam.Start()
End Sub
Private Sub bntStop_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnStop.Click
webcam.[Stop]()
End Sub
Private Sub bntContinue_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnContinue.Click
webcam.[Continue]()
End Sub
Private Sub bntCapture_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCapture.Click
imgCapture.Image = imgVideo.Image
End Sub
Private Sub bntSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
SaveImageCapture(imgCapture.Image)
End Sub
Private Sub bntVideoFormat_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnVideoFormat.Click
webcam.ResolutionSetting()
End Sub
Private Sub bntVideoSource_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnVideoSource.Click
webcam.AdvanceSetting()
End Sub
Public Shared Sub SaveImageCapture(ByVal image As System.Drawing.Image)
Dim s As SaveFileDialog = New SaveFileDialog()
s.FileName = "Image"
s.DefaultExt = ".Jpg"
s.Filter = "Image (.jpg)|*.jpg"
Dim filePath As String = Path.GetDirectoryName(Application.ExecutablePath) & "\File\"
If Not Directory.Exists(filePath) Then
Directory.CreateDirectory(filePath)
End If
Dim filename As String = filePath & "Image.jpg"
Dim fstream As FileStream = New FileStream(filename, FileMode.Create)
image.Save(fstream, System.Drawing.Imaging.ImageFormat.Jpeg)
fstream.Close()
Using bmp As Bitmap = New Bitmap(filename, False)
Using grp As Graphics = Graphics.FromImage(bmp)
Dim brush As Brush = New SolidBrush(Color.White)
Dim font As Font = New System.Drawing.Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel)
Dim textSize As SizeF = New SizeF()
textSize = grp.MeasureString("@ASPSnippets.com", font)
Dim position As Point = New Point((bmp.Width - (CInt(textSize.Width) + 10)), (bmp.Height - (CInt(textSize.Height) + 10)))
grp.DrawString("@ASPSnippets.com", font, brush, position)
End Using
If s.ShowDialog() = DialogResult.OK Then
bmp.Save(s.FileName)
End If
End Using
If Directory.Exists(filePath) Then
Directory.Delete(filePath, True)
End If
End Sub
End Class