Hi Bhavesh23,
Using the below article i have created the example.
HTML
Home
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<th align="center"><u>Live Camera</u></th>
<th align="center"><u>Captured Picture</u></th>
</tr>
<tr>
<td>
<div id="webcam"></div>
</td>
<td>
<img id="imgCapture" /></td>
</tr>
<tr>
<td align="center">
<input type="button" id="btnCapture" value="Capture" />
</td>
<td align="center">
<input type="button" id="btnSend" value="Send" disabled="disabled" />
</td>
</tr>
</table>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="WebCam.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
Webcam.set({
width: 320,
height: 240,
image_format: 'jpeg',
jpeg_quality: 90
});
Webcam.attach('#webcam');
$("#btnCapture").click(function () {
Webcam.snap(function (data_uri) {
$("#imgCapture")[0].src = data_uri;
$("#btnSend").removeAttr("disabled");
});
});
$("#btnSend").click(function () {
$.ajax({
type: "POST",
url: "Home.aspx/SaveCapturedImage",
data: "{data: '" + $("#imgCapture")[0].src + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
window.location.href = '/Default.aspx';
}
});
});
});
</script>
Default
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center">
<u>Captured Picture</u>
</td>
</tr>
<tr>
<td>
<asp:Image ID="imgCapture" runat="server" Style="width: 320px; height: 240px" />
</td>
</tr>
</table>
Namespaces
C#
using System.IO;
using System.Web.Services;
VB.Net
Imports System.IO
Imports System.Web.Services
Code
C#
Home
[WebMethod()]
public static bool SaveCapturedImage(string data)
{
string fileName = DateTime.Now.ToString("dd-MM-yy hh-mm-ss");
//Convert Base64 Encoded string to Byte Array.
byte[] imageBytes = Convert.FromBase64String(data.Split(',')[1]);
//Save the Byte Array as Image File.
string filePath = HttpContext.Current.Server.MapPath(string.Format("~/Captures/{0}.jpg", fileName));
File.WriteAllBytes(filePath, imageBytes);
HttpContext.Current.Session["CapturedImage"] = string.Format("~/Captures/{0}.jpg", fileName);
return true;
}
Default
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (Session["CapturedImage"] != null)
{
imgCapture.ImageUrl = Session["CapturedImage"].ToString();
Session["CapturedImage"] = null;
}
}
}
VB.Net
Home
<WebMethod()>
Public Shared Function SaveCapturedImage(ByVal data As String) As Boolean
Dim fileName As String = DateTime.Now.ToString("dd-MM-yy hh-mm-ss")
Dim imageBytes As Byte() = Convert.FromBase64String(data.Split(","c)(1))
Dim filePath As String = HttpContext.Current.Server.MapPath(String.Format("~/Captures/{0}.jpg", fileName))
File.WriteAllBytes(filePath, imageBytes)
HttpContext.Current.Session("CapturedImage") = String.Format("~/Captures/{0}.jpg", fileName)
Return True
End Function
Default
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
If Session("CapturedImage") IsNot Nothing Then
imgCapture.ImageUrl = Session("CapturedImage").ToString()
Session("CapturedImage") = Nothing
End If
End If
End Sub