Bhavesh Vankar

Bhavesh Vankar

  • 761
  • 1.1k
  • 88k

Capture Image by webcam getting error

Sep 10 2020 8:00 AM
In my project i want to capture image all done but now its generating error and image not capturing below is my code kindly help me to solve the error.
image not getting in folder using below code. kindly please solve and suggest me good code. 
 
on my captureimage.aspx
 
jquery code for capture image
  1. <script type="text/javascript">  
  2.         var pageUrl = '<%=ResolveUrl("~/captureimage.aspx") %>';  
  3.         $(function () {  
  4.             Webcam.set({  
  5.                 width: 320,  
  6.                 height: 240,  
  7.                 image_format: 'jpeg',  
  8.                 jpeg_quality: 90  
  9.             });  
  10.             Webcam.attach('#webcam');  
  11.             $("#btnCapture").click(function () {  
  12.                 Webcam.snap(function (data_uri) {  
  13.                     $("#imgCapture")[0].src = data_uri;  
  14.                     $("#btnUpload").removeAttr("disabled");  
  15.                 });  
  16.             });  
  17.             $("#btnCapture").click(function () {  
  18.                 $.ajax({  
  19.                     type: "POST",  
  20.                     url: pageUrl + "captureimage.aspx/GetCapturedImage",  
  21.                     data: "{data: '" + $("#imgCapture")[0].src + "'}",  
  22.                     contentType: "application/json; charset=utf-8",  
  23.                     dataType: "json",  
  24.                     success: function (r)  
  25.                     {  
  26.                         $("[id*=imgCapture]").css("visibility""visible");  
  27.                         $("[id*=imgCapture]").attr("src", r.d);  
  28.                     },  
  29.                     failure: function (response)  
  30.                     {  
  31.                         alert(response.d);  
  32.                     },  
  33.                     oncapture: function ()  
  34.                     {  
  35.                         webcam.save(pageUrl);  
  36.                     }  
  37.                 });  
  38.             });  
  39.         });  
  40.         function capture()  
  41.         {  
  42.             webcam.capture();  
  43.             return false;  
  44.         }  
  45.   
  46.     </script>  
my code in captureimage.cs file...
 
 
 
  1. protected void Page_Load(object sender, EventArgs e)  
  2.         {  
  3.             if (!this.IsPostBack)  
  4.             {  
  5.                 if (Request.InputStream.Length > 0)  
  6.                 {  
  7.                     using (StreamReader myreader = new StreamReader(Request.InputStream))  
  8.                     {  
  9.                         string myhexString = Server.UrlEncode(myreader.ReadToEnd());  
  10.                         string myimageName = DateTime.Now.ToString("Image.jpg");  
  11.                         string myimagePath = string.Format ("~/CapturedImage/Image.jpg", myimageName);  
  12.                         File.WriteAllBytes(Server.MapPath(myimagePath), ConvertHexToBytes(myhexString));  
  13.                         Session["visitorimage"] =  ResolveUrl(myimagePath);  
  14.                     }  
  15.                 }  
  16.             }  
  17.         }  
  18.         private static byte[] ConvertHexToBytes(string hex)  
  19.         {  
  20.             byte[] bytes = new byte[hex.Length / 2];  
  21.             for (int i = 0; i < hex.Length; i += 2)  
  22.             {  
  23.                 bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);  
  24.             }  
  25.             return bytes;  
  26.         }  
  27.   
  28.   
  29.         [WebMethod(EnableSession = true)]  
  30.         public static string GetCapturedImage()  
  31.         {  
  32.             string Imageurl = HttpContext.Current.Session ["visitorimage"].ToString();  
  33.             HttpContext.Current.Session["visitorimage"] = null;  
  34.             return Imageurl;  
  35.         }  
While Click on  capture button occure error 
"An exception of type 'System.IO.FileNotFoundException' occurred in System.Drawing.dll but was not handled in user code "
 
Crop code is..
  1. protected void btnCrop_Click(object sender, EventArgs e)  
  2.         {  
  3.             string FilePath = Path.Combine("~/CapturedImage/Image.jpg");  
  4.             int x = Convert.ToInt32(X.Value);  
  5.             int y = Convert.ToInt32(Y.Value);  
  6.             int w = Convert.ToInt32(W.Value);  
  7.             int h = Convert.ToInt32(H.Value);  
  8.             //System.Drawing.Image image = Bitmap.FromFile(HttpContext.Current.Request.PhysicalApplicationPath + @"\CapturedImage\" + DateTime.Now.ToString("dd-MM-yy hh-mm") + ".jpg");  
  9.             System.Drawing.Image image = Bitmap.FromFile(HttpContext.Current.Request.PhysicalApplicationPath + @"\CapturedImage\Image.jpg");  
  10.             Bitmap bmp = new Bitmap(w, h, image.PixelFormat);  
  11.             Graphics g = Graphics.FromImage(bmp);  
  12.             g.DrawImage(image, new Rectangle(0, 0, w, h), new Rectangle(x, y, w, h), GraphicsUnit.Pixel);  
  13.             //bmp.Save(HttpContext.Current.Request.PhysicalApplicationPath + @"\CroppedImage\" + DateTime.Now.ToString("dd-MM-yy hh-mm") + ".jpg", image.RawFormat);  
  14.             bmp.Save(HttpContext.Current.Request.PhysicalApplicationPath + @"\CroppedImage\Image.jpg", image.RawFormat);  
  15.             //imgCapture.ImageUrl = @"\CroppedImage\" + DateTime.Now.ToString("dd-MM-yy hh-mm") + ".jpg";  
  16.             imgCapture.ImageUrl = "~/CroppedImage/Image.jpg";  
  17.         }  
 
 

Answers (3)