ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Capture Image from Webcam and Save in Database in binary format in ASP.Net<p>Hi&nbsp;<a class="username" rel="Waghmare">Waghmare,</a></p> <p>From the&nbsp;captured image you need to get the byte data and&nbsp;and save in database in binary format.</p> <h1 class="header"><a title="Capture Image (Photo) from Web Camera (Webcam) in ASP.Net using C# and VB.Net'a" href="https://www.aspsnippets.com/Articles/Capture-Image-Photo-from-Web-Camera-Webcam-in-ASPNet-using-C-and-VBNet.aspx">Capture Image (Photo) from Web Camera (Webcam) in ASP.Net using C# and VB.Net</a></h1> <p>In the above article below line is&nbsp;saving bytes as file.</p> <pre class="brush: csharp">File.WriteAllBytes(Server.MapPath(imagePath), ConvertHexToBytes(hexString));</pre> <p>Instead you can get the bytes using the below code.</p> <pre class="brush: csharp">byte[] bytes = ConvertHexToBytes(hexString);</pre> <p>Then&nbsp;save in database. Refer below article for saving byte data in database.</p> <h1 class="header"><a title="Read and Write BLOB Data to SQL Server database using C# and VB.Net'a" href="https://www.aspsnippets.com/Articles/Read-and-Write-BLOB-Data-to-SQL-Server-database-using-C-and-VBNet.aspx">Read and Write BLOB Data to SQL Server database using C# and VB.Net</a></h1>https://www.aspforums.net:443/Threads/210776/Capture-Image-from-Webcam-and-Save-in-Database-in-binary-format-in-ASPNet/https://www.aspforums.net:443/Threads/210776/Capture-Image-from-Webcam-and-Save-in-Database-in-binary-format-in-ASPNet/Tue, 08 May 2018 05:47:23 GMTCapture Image from Webcam and Save in Database in binary format in ASP.Net<p>Check the below sample code For your reference and implement it as per your code logic.</p> <p><span style="text-decoration: underline;"><strong>SQL</strong></span></p> <pre class="brush: 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</pre> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head id=&#34;Head1&#34; runat=&#34;server&#34;&gt; &lt;title&gt;&lt;/title&gt; &lt;style type=&#34;text/css&#34;&gt; body { font-family: Arial; font-size: 10pt; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script src=&#39;&lt;%=ResolveUrl(&#34;~/Webcam_Plugin/jquery.webcam.js&#34;) %&gt;&#39; type=&#34;text/javascript&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; var pageUrl = &#39;&lt;%=ResolveUrl(&#34;~/Default.aspx&#34;) %&gt;&#39;; $(function () { jQuery(&#34;#webcam&#34;).webcam({ width: 320, height: 240, mode: &#34;save&#34;, swffile: &#39;&lt;%=ResolveUrl(&#34;~/Webcam_Plugin/jscam.swf&#34;) %&gt;&#39;, debug: function (type, status) { $(&#39;#camStatus&#39;).append(type + &#34;: &#34; + status + &#39;&lt;br /&gt;&lt;br /&gt;&#39;); }, onSave: function (data) { $.ajax({ type: &#34;POST&#34;, url: pageUrl + &#34;/GetCapturedImage&#34;, data: &#39;&#39;, contentType: &#34;application/json; charset=utf-8&#34;, dataType: &#34;json&#34;, success: function (r) { $(&#34;[id*=imgCapture]&#34;).css(&#34;visibility&#34;, &#34;visible&#34;); $(&#34;[id*=imgCapture]&#34;).attr(&#34;src&#34;, r.d); }, failure: function (response) { alert(response.d); } }); }, onCapture: function () { webcam.save(pageUrl); } }); }); function Capture() { webcam.capture(); return false; } &lt;/script&gt; &lt;form id=&#34;form1&#34; runat=&#34;server&#34;&gt; &lt;table border=&#34;0&#34; cellpadding=&#34;0&#34; cellspacing=&#34;0&#34;&gt; &lt;tr&gt; &lt;td align=&#34;center&#34;&gt; &lt;u&gt;Live Camera&lt;/u&gt; &lt;/td&gt; &lt;td&gt; &lt;/td&gt; &lt;td align=&#34;center&#34;&gt; &lt;u&gt;Captured Picture&lt;/u&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;div id=&#34;webcam&#34;&gt; &lt;/div&gt; &lt;/td&gt; &lt;td&gt; &amp;nbsp; &lt;/td&gt; &lt;td&gt; &lt;asp:Image ID=&#34;imgCapture&#34; runat=&#34;server&#34; Style=&#34;visibility: hidden; width: 320px; height: 240px&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;br /&gt; &lt;asp:Button ID=&#34;btnCapture&#34; Text=&#34;Capture&#34; runat=&#34;server&#34; OnClientClick=&#34;return Capture();&#34; /&gt; &lt;br /&gt; &lt;span id=&#34;camStatus&#34;&gt;&lt;/span&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p><span style="text-decoration: underline;"><strong>C#</strong></span></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { if (Request.InputStream.Length &gt; 0) { using (StreamReader reader = new StreamReader(Request.InputStream)) { string contentType = Request.ContentType; string hexString = Server.UrlEncode(reader.ReadToEnd()); string imageName = DateTime.Now.ToString(&#34;dd-MM-yy hh-mm-ss&#34;); string imagePath = string.Format(&#34;~/Captures/{0}.png&#34;, imageName); // UnComment this part if needed to save in Folder //File.WriteAllBytes(Server.MapPath(imagePath), ConvertHexToBytes(hexString)); Session[&#34;CapturedImage&#34;] = ResolveUrl(imagePath); byte[] bytes = ConvertHexToBytes(hexString); string constring = ConfigurationManager.ConnectionStrings[&#34;ConStr&#34;].ConnectionString; SqlConnection con = new SqlConnection(constring); string strQuery = &#34;insert into tblFiles(Name, ContentType, Data) values (@Name, @ContentType, @Data)&#34;; SqlCommand cmd = new SqlCommand(strQuery, con); cmd.CommandType = CommandType.Text; con.Open(); cmd.Parameters.Add(&#34;@Name&#34;, SqlDbType.VarChar).Value = imageName; cmd.Parameters.Add(&#34;@ContentType&#34;, SqlDbType.VarChar).Value = contentType; cmd.Parameters.Add(&#34;@Data&#34;, 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 &lt; 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[&#34;CapturedImage&#34;].ToString(); HttpContext.Current.Session[&#34;CapturedImage&#34;] = null; return url; }</pre> <p><span style="text-decoration: underline;"><strong>VB.Net</strong></span></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then If Request.InputStream.Length &gt; 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(&#34;dd-MM-yy hh-mm-ss&#34;) Dim imagePath As String = String.Format(&#34;~/Captures/{0}.png&#34;, imageName) &#39;UnComment this part if needed to save in Folder &#39;File.WriteAllBytes(Server.MapPath(imagePath), ConvertHexToBytes(hexString)) Session(&#34;CapturedImage&#34;) = ResolveUrl(imagePath) Dim bytes As Byte() = ConvertHexToBytes(hexString) Dim constring As String = ConfigurationManager.ConnectionStrings(&#34;ConStr&#34;).ConnectionString Dim con As SqlConnection = New SqlConnection(constring) Dim strQuery As String = &#34;insert into tblFiles(Name, ContentType, Data) values (@Name, @ContentType, @Data)&#34; Dim cmd As SqlCommand = New SqlCommand(strQuery, con) cmd.CommandType = CommandType.Text con.Open() cmd.Parameters.Add(&#34;@Name&#34;, SqlDbType.VarChar).Value = imageName cmd.Parameters.Add(&#34;@ContentType&#34;, SqlDbType.VarChar).Value = contentType cmd.Parameters.Add(&#34;@Data&#34;, 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 &lt;WebMethod(EnableSession:=True)&gt; _ Public Shared Function GetCapturedImage() As String Dim url As String = HttpContext.Current.Session(&#34;CapturedImage&#34;).ToString() HttpContext.Current.Session(&#34;CapturedImage&#34;) = Nothing Return url End Function</pre>https://www.aspforums.net:443/Threads/210776/Capture-Image-from-Webcam-and-Save-in-Database-in-binary-format-in-ASPNet/https://www.aspforums.net:443/Threads/210776/Capture-Image-from-Webcam-and-Save-in-Database-in-binary-format-in-ASPNet/Wed, 09 May 2018 07:16:16 GMT