Bhavesh Vankar

Bhavesh Vankar

  • 755
  • 1.2k
  • 89.2k

Capture Image using Asp.net Button C#

Jan 2 2021 7:42 AM
When i try to capture image using asp.net button the image not capturing by webcam. and when i capture by html button its capturing image. i want to use asp.net button. what to do in this case...
my code is below...
This is my webmethod to capture image by webcam. 
 
  1. [WebMethod()]  
  2.        public static bool SaveCapturedImage(string data)  
  3.        {  
  4.            //string hostName = Dns.GetHostName();  
  5.            //string myIP = Dns.GetHostByName(hostName).AddressList[0].ToString();  
  6.            //myIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];  
  7.            //if (myIP == "" || myIP == null)  
  8.            //{  
  9.            //    myIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];  
  10.            //}  
  11.           // string fileName = myIP + DateTime.Now.ToString("dd-MM-yy_hh-mm");  
  12.            string fileName = DateTime.Now.ToString("dd-MM-yy_hh-mm");  
  13.            byte[] imageBytes = Convert.FromBase64String(data.Split(',')[1]);  
  14.            //Save the Byte Array as Image File.  
  15.            string filePath = HttpContext.Current.Server.MapPath(string.Format("~/CapturedImage/{0}.jpg", fileName));  
  16.            File.WriteAllBytes(filePath, imageBytes);  
  17.            return true;  
  18.        }  
javascript code to capture image....
 
 
  1. <script type="text/javascript">  
  2.         $(function () {  
  3.             Webcam.set({  
  4.                 width: 320,  
  5.                 height: 240,  
  6.                 image_format: 'jpeg',  
  7.                 jpeg_quality: 90  
  8.             });  
  9.             Webcam.attach('#webcam');  
  10.             $("#btnCapture").click(function () {  
  11.                 Webcam.snap(function (data_uri) {  
  12.                     $("#imgCapture")[0].src = data_uri;  
  13.                     $("#btnUpload").removeAttr("disabled");  
  14.                 });  
  15.             });  
  16.             $("#btnCapture").click(function () {  
  17.                 $.ajax({  
  18.                     type: "POST",  
  19.                     url: "captureimage.aspx/SaveCapturedImage",  
  20.                     data: "{data: '" + $("#imgCapture")[0].src + "'}",  
  21.                     contentType: "application/json; charset=utf-8",  
  22.                     dataType: "json",  
  23.                     success: function (r) {  
  24.                         windo.close();  
  25.                         window.location.href = '/studentpage.aspx';  
  26.                     }  
  27.                 });  
  28.             });  
  29.         });  
  30.   
  31.     </script>  
 
 
 This is my button code....
i have commented the as.net button.  
  1. <input type="button" id="btnCapture" value="Capture" class="buttondesign" />  
  2.                    <%--<asp:Button ID="btnCapture" runat="server" Text="Capture" CssClass="buttondesign" OnClick="btnCapture_Click1" />--%>  
  3.                   
 

Answers (1)