1

How can I create a webcam snapshot button in my website so the user can take a photo and upload the taken photo to my server?

I am using a C# web application; please help me with some links or code.

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
bassem ala
  • 181
  • 1
  • 13
  • 1
    You have a bunch of questions that have been answered and has obviously helped you, but you have not [accepted them](http://u.sbhat.me/t6SXUH). Please do else people may be not be inclined to help you. – Sathyajith Bhat May 09 '12 at 09:38

1 Answers1

3

You could use Flash to access the webcam. Here's a jquery plugin that you could use and which greatly simplifies its manipulation using javascript.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • yup i saw this article but this is written in php code can u give me a c# code so i can use it plz – bassem ala May 09 '12 at 08:59
  • I wrote an example with ASP.NET MVC here: http://stackoverflow.com/a/9452030/29407 – Darin Dimitrov May 09 '12 at 09:13
  • but i'm not using MVC i am just using an aspx page how can i let this work ? please help me – bassem ala May 09 '12 at 09:28
  • It's the same thing. The ASPX page that you are invoking will be passed a parameter called `image`. So all you have to do is read this parameter from the request : `Request["image"]` and then apply the same code. – Darin Dimitrov May 09 '12 at 09:29
  • then i guess i should put that code in my aspx page ? [HttpPost] public ActionResult Upload(string image) { image = image.Substring("data:image/png;base64,".Length); var buffer = Convert.FromBase64String(image); // TODO: I am saving the image on the hard disk but // you could do whatever processing you want with it System.IO.File.WriteAllBytes(Server.MapPath("~/app_data/capture.png"), buffer); return Json(new { success = true }); } and the image shld be Request["image"]; ?? – bassem ala May 09 '12 at 09:43
  • Yes, you should put it in the code behind of your ASPX page. But don't just blindly copy-paste the code of my previous answer. It is ASP.NET MVC. Or even better, write a generic ASHX handler to process your photo uploads. I would recommend you reading some ASP.NET tutorials as I see that you are lacking some very basic notions. – Darin Dimitrov May 09 '12 at 09:44
  • can u give me a complete code please ... i'm really sorry but i'm completely new at this – bassem ala May 09 '12 at 09:48
  • 1
    @bassemala, I prefer not to write full source code here because people do not learn anything like that. I would recommend you reading some getting started ASP.NET tutorials. The idea here is to read the `image` parameter inside your webform and then simply convert it to a byte array using the `Convert.FromBase64String` method. What you do with this byte array is then up to you: you could save the image on disk or somewhere else. So try implementing it first and then if you encounter some problems don't hesitate to show your progress and explain what difficulties you encountered with the code. – Darin Dimitrov May 09 '12 at 09:49
  • 1
    But simply do not expect me doing the job for you without me seeing some efforts on your side. Or maybe you could wait for someone else to write the complete code for you. – Darin Dimitrov May 09 '12 at 09:51
  • no i don't mean it that way i am sorry the point is i tried but i can't manage to get the image object created after the snapshot button is clicked – bassem ala May 09 '12 at 09:53
  • can u please just explain that line : $.post("/upload.php", {type: "pixel", image: image.join('|')}); – bassem ala May 09 '12 at 11:01
  • It sends an AJAX POST request to the `/upload.php` server side script sending 2 parameters: `type` and `image`. The type parameter value is hardcoded to `pixel` and the image parameter represents a string in which the image array values are concatenated with the `|` character. – Darin Dimitrov May 09 '12 at 11:33
  • alright i think i can manage 2 solve it i will send an AJAX Call to my web service page containing the image than save it into a folder and Done i'll give it a try thank you very much for your support – bassem ala May 09 '12 at 11:48