Check the below sample code For your reference and implement it as per your code logic.
SQL
CREATE TABLE [tblFiles](
[id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NOT NULL,
[ContentType] [nvarchar](200) NOT NULL,
[Data] [varbinary](max) NOT NULL,
CONSTRAINT [PK_tblFiles] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
</style>
</head>
<body>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src='<%=ResolveUrl("~/Webcam_Plugin/jquery.webcam.js") %>' type="text/javascript"></script>
<script type="text/javascript">
var pageUrl = '<%=ResolveUrl("~/Default.aspx") %>';
$(function () {
jQuery("#webcam").webcam({
width: 320,
height: 240,
mode: "save",
swffile: '<%=ResolveUrl("~/Webcam_Plugin/jscam.swf") %>',
debug: function (type, status) {
$('#camStatus').append(type + ": " + status + '<br /><br />');
},
onSave: function (data) {
$.ajax({
type: "POST",
url: pageUrl + "/GetCapturedImage",
data: '',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
$("[id*=imgCapture]").css("visibility", "visible");
$("[id*=imgCapture]").attr("src", r.d);
},
failure: function (response) {
alert(response.d);
}
});
},
onCapture: function () {
webcam.save(pageUrl);
}
});
});
function Capture() {
webcam.capture();
return false;
}
</script>
<form id="form1" runat="server">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center">
<u>Live Camera</u>
</td>
<td>
</td>
<td align="center">
<u>Captured Picture</u>
</td>
</tr>
<tr>
<td>
<div id="webcam">
</div>
</td>
<td>
</td>
<td>
<asp:Image ID="imgCapture" runat="server" Style="visibility: hidden; width: 320px;
height: 240px" />
</td>
</tr>
</table>
<br />
<asp:Button ID="btnCapture" Text="Capture" runat="server" OnClientClick="return Capture();" />
<br />
<span id="camStatus"></span>
</form>
</body>
</html>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (Request.InputStream.Length > 0)
{
using (StreamReader reader = new StreamReader(Request.InputStream))
{
string contentType = Request.ContentType;
string hexString = Server.UrlEncode(reader.ReadToEnd());
string imageName = DateTime.Now.ToString("dd-MM-yy hh-mm-ss");
string imagePath = string.Format("~/Captures/{0}.png", imageName);
// UnComment this part if needed to save in Folder
//File.WriteAllBytes(Server.MapPath(imagePath), ConvertHexToBytes(hexString));
Session["CapturedImage"] = ResolveUrl(imagePath);
byte[] bytes = ConvertHexToBytes(hexString);
string constring = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
SqlConnection con = new SqlConnection(constring);
string strQuery = "insert into tblFiles(Name, ContentType, Data) values (@Name, @ContentType, @Data)";
SqlCommand cmd = new SqlCommand(strQuery, con);
cmd.CommandType = CommandType.Text;
con.Open();
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = imageName;
cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = contentType;
cmd.Parameters.Add("@Data", SqlDbType.VarBinary).Value = bytes;
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
private static byte[] ConvertHexToBytes(string hex)
{
byte[] bytes = new byte[hex.Length / 2];
for (int i = 0; i < hex.Length; i += 2)
{
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
}
return bytes;
}
[WebMethod(EnableSession = true)]
public static string GetCapturedImage()
{
string url = HttpContext.Current.Session["CapturedImage"].ToString();
HttpContext.Current.Session["CapturedImage"] = null;
return url;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
If Request.InputStream.Length > 0 Then
Using reader As StreamReader = New StreamReader(Request.InputStream)
Dim contentType As String = Request.ContentType
Dim hexString As String = Server.UrlEncode(reader.ReadToEnd())
Dim imageName As String = DateTime.Now.ToString("dd-MM-yy hh-mm-ss")
Dim imagePath As String = String.Format("~/Captures/{0}.png", imageName)
'UnComment this part if needed to save in Folder
'File.WriteAllBytes(Server.MapPath(imagePath), ConvertHexToBytes(hexString))
Session("CapturedImage") = ResolveUrl(imagePath)
Dim bytes As Byte() = ConvertHexToBytes(hexString)
Dim constring As String = ConfigurationManager.ConnectionStrings("ConStr").ConnectionString
Dim con As SqlConnection = New SqlConnection(constring)
Dim strQuery As String = "insert into tblFiles(Name, ContentType, Data) values (@Name, @ContentType, @Data)"
Dim cmd As SqlCommand = New SqlCommand(strQuery, con)
cmd.CommandType = CommandType.Text
con.Open()
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = imageName
cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = contentType
cmd.Parameters.Add("@Data", SqlDbType.VarBinary).Value = bytes
cmd.ExecuteNonQuery()
con.Close()
End Using
End If
End If
End Sub
Private Shared Function ConvertHexToBytes(hex As String) As Byte()
Dim bytes As Byte() = New Byte(hex.Length / 2 - 1) {}
For i As Integer = 0 To hex.Length - 1 Step 2
bytes(i / 2) = Convert.ToByte(hex.Substring(i, 2), 16)
Next
Return bytes
End Function
<WebMethod(EnableSession:=True)> _
Public Shared Function GetCapturedImage() As String
Dim url As String = HttpContext.Current.Session("CapturedImage").ToString()
HttpContext.Current.Session("CapturedImage") = Nothing
Return url
End Function