Capture and save images from Web camera using C# and ASP.NET


This C# code sample explains how to capture and save images or videos directly from your web camera. This web camera functionality is implemented using flash and C# and ASP.NET. Learn more about how to capture images from webcamera using .NET and save in to your computer.

In this article with some C# sample, I am going to explain how to capture and save image from web camera.

This web camera capturing and storing is implemented using flash on the front end and C# on the back end. You can easily embed this flash file into your web page and record images from the webcam.

Requirements to capture images from webcam using C#:


If you want to implement this functionality you need to install the latest flash player and have a working web camera.

Steps to capture webcam images using C# and ASP.NET:


1) Create a new web application project and copy the WebcamResources folder from the attachments.

2) Create a new page named "Default.aspx" page add the following code,


<object width="405" height="190"
param name="movie" value="WebcamResources/save_picture.swf"
embed src="WebcamResources/save_picture.swf" width="405" height="190" >
</object>


The above code is to place the flash object in your webpage, which is used to capture images from the webcam.

3) Create another ASP.NET web page and name it "ImageConversions.aspx"

This page name is should be of same name if you use the attached swf file. Whenever the capture image button is clicked, then it redirectd
to "ImageConversions.aspx" page. So the file name matters!

4) In this ASP.NET page, add the following C# code in the pageload or create a
separate method.


string strPhoto = Request.Form["imageData"]; //Get the image from flash file
byte[] photo = Convert.FromBase64String(strPhoto);
FileStream fs = new FileStream("C:\\Webcam.jpg", FileMode.OpenOrCreate, FileAccess.Write);
BinaryWriter br = new BinaryWriter(fs);
br.Write(photo);
br.Flush();
br.Close();
fs.Close();


Above example written in C# will convert the byte code into image and store the image in local C: drive. Even though the code sample is given in C#, if you are a VB.NET guy, you can easily convert this to VB.NET and write your web cam application in VB.NET.

See the attached application which can be used to save images from the webcam. Download the files and add to your Visual Studio project and start capturing pictures from your webcam.

Do you have any questions or suggestions to this web application to capture images from web camera? Feel free to post your comments below and I will be more than happy to respond to your questions.


Attachments

  • Capture and save images from Web camera (38150-4106-WebcamCapture.rar)
  • Comments

    Author: Raju Melveetilpurayil04 May 2010 Member Level: Gold   Points : 0

    good one am thinking for this.

    anyway keep posting.

    Author: DAMU27 Apr 2011 Member Level: Gold   Points : 1

    Hi Sanjay,

    Sorry for a long delayed response. Did you get the solution?

    Can you clear your requirements?

    Author: DAMU18 Jul 2011 Member Level: Gold   Points : 0

    Hi Archana,

    Check your webcam settings. Could you please send me your complete solution?

    Author: DAMU18 Jul 2011 Member Level: Gold   Points : 1

    Hi Archana,

    I think your web camera is not working fine. Please verify your web camera settings, before running your application.

    Regards,
    Damu

    Author: BULLICH21 Jul 2011 Member Level: Bronze   Points : 1

    Hi Damu and thanks a lot for this post.
    The image is always 200 x 150 ; how can i chnage this to 320 x 240?
    thx
    manu

    Author: DAMU21 Jul 2011 Member Level: Gold   Points : 1

    Hi Bullich,

    You need to change the image size in this "save_picture.fla" flash file. You can find this file under the "Web cam resources" folder.

    Regards,
    Damu

    Author: Pawan Awasthi21 Jul 2011 Member Level: Gold   Points : 1

    Hai Damu,
    This is nice article..very useful..
    keep it up..keep on sending more and more article so that i can learn more and more..

    Author: BULLICH22 Jul 2011 Member Level: Bronze   Points : 0

    thanks damu.
    I changed the fla and if works perfect.
    thx
    manu

    Author: DAMU22 Jul 2011 Member Level: Gold   Points : 0

    Always welcome Bullich.

    Guest Author: nilesh01 Dec 2011

    This is really great article. I want to capture 3 images through webcam on userprofile page(main page). I have created 3 hyperlinks on userprofile page and calling this WebcamResources/default.aspx on each link. I capture the image and store image in server folder. Till here I have no issues in application. But I want to pass file name from userprofile page to WebcamResources/default.aspx so Image will store with same name in server folder. Or whatever file name define in imageconversion.aspx file should be assigned back to userprofile page. So I will come to know which image to be stored with which link in database. I tried a lot but I can not pass or read value from webcam page. Please help..Thanks..

    Author: BULLICH02 Dec 2011 Member Level: Bronze   Points : 0

    hi damu,
    I am looking for the same thing to capture a scan from a scanner. Kind of "twain flash object" to put in the asp.net page...
    Any clue?
    thx
    manu

    Guest Author: somesh,purushotham12 Dec 2011

    I used ur article.Its working fine.but I have some problem on
    Application access another PC .Capture Image on another PC(web cam connected) but image not saved in given path(server path).

    Eg. FileStream fs = new FileStream("C:\\Webcam.jpg", FileMode.OpenOrCreate, FileAccess.Write);

    This is done in server pc.
    pls suggest the solution on how to capture image from another system also..

    Guest Author: Art03 Jan 2012

    Article is great, but when I used this code , working on IE and Mozilla, but in Chrome showing Missing Plugin, so if you have solution please provide me.

    Author: shahnawaz06 Jan 2012 Member Level: Silver   Points : 0

    hi,
    images has to convert into binary formate then it will save.

    Guest Author: 13 Jan 2012

    Hi DAMU , Thanks for this post.
    but it seems swf file is not working in compatibility mode. can you please advise for this.

    Rohit

    Guest Author: Neel20 Jan 2012

    hi i am new to .net. I have the same requirement as above. i want to take image from web cam if available then store in sql server. could u pls guide me to this?

    Guest Author: Neel20 Jan 2012

    Hi, Is it possible to save images to sql server in database. Please guide me. How can i access your code from an aspx page in application.

    Guest Author: 20 Jan 2012

    hi guys,

    I'm a begginer on using C# so .. I'm facing the folowing problem.
    I have a form into a application and I want to receive the Image into a img space in my form... like this... clicking in a button a window or a frame opens with the code above and instead of recording a file I want to receive a IMG into my [img...id=photo runat=server...] then I can SAVE my image with the rest of the form data.

    does it make sense ?
    or it could be something like
    [img src="http://....aspx"]
    jorgesomers at gmail dot com

    Author: DAMU24 Jan 2012 Member Level: Gold   Points : 0

    Hi Jorge,

    No. It's not good way and very slow process. You can find out any other api for video recording or streaming.

    Guest Author: Lolita31 Jan 2012

    hello..
    Thanks for your code.. Its really good..In tis code the image s storing in c drive .. so how can i store tis in database..Can u pls get code for d task..

    Guest Author: Hari03 Feb 2012

    Hi DAMU,
    Thank you for the outstanding article.
    i need to avoid "server is requesting webcam device, allow deny" message.
    where i have to change the settings.

    Guest Author: chen08 Feb 2012

    Hi, Is there any way to resize the image to a higher resolution? What I want is the browser to show a 200x150, but for the saved image to be 640x480.

    Guest Author: FilipeMcy29 Feb 2012

    Hello! I wonder if there are any specific configuration for this solution is implemented through a server? or another version of the file Base64.as?

    I'm using it on a project, and when I run local host it works, but when I run from the server that does not get the image of the flash file.

    Thank you!

    Author: Nilesh Kshetre01 Mar 2012 Member Level: Bronze   Points : 0

    ho to store image in server folder plz help me ?

    Guest Author: 08 Mar 2012

    i m getting error at
    byte[] photo = Convert.FromBase64String(strPhoto);


    error:Value cannot be null.
    Parameter name: InString

    Guest Author: Rocky15 Mar 2012

    Many people commented that it is working ,, in my case i am not able to make this code work ,,, could you elaborate the article a bit more ...

    Guest Author: Khushboo31 Mar 2012

    Hello Sir,
    I implemented the code as u described, it is capturing the image but not saving it..It is not redirecting the control to the page ImageConversions.aspx. Please help me its urgent.

    Guest Author: 18 Apr 2012

    Hi

    I am using your code in a model popup to capture the Photo,But the capture event in not fired. Please help

    Author: DAMU18 Apr 2012 Member Level: Gold   Points : 0

    Hi Rocky,

    Post your errors or problem which are facing while implementing this source. Then, only I can able to help to solve the errors.

    Guest Author: taniya19 Apr 2012

    this mgsbox(error) is being displayed
    "the path C:\IECapture does not exist or not a directory"

    plz tell me solution

    Guest Author: veenaravi23 Apr 2012

    hi, i am not getting the webcam image(Object) but i have an webcam but in silverlight i am getting the webcam image please help me............

    Guest Author: anil kumar01 May 2012

    hi sir it good but how save this image into database

    Guest Author: Satya04 May 2012

    to change the image size in this "save_picture.fla" flash file.
    I tried to opened .fla file in adobe flash cs3 professional & Macromedia flash but its showing me same error Unexpected file format, will any one help on this issue I need it urgent.
    Thank u
    mail:satya.techsmart@gmail.com

    Guest Author: HARIKRISHNAN06 May 2012

    What should be done for capturing videos. Pls help

    Author: DAMU07 May 2012 Member Level: Gold   Points : 0

    Hi Satya,

    Give to your flash action script designers to do change the image size. I don't know the flash action scripts.

    Guest Author: Thai08 May 2012

    Can i call capture with javascript function?

    Author: DAMU09 May 2012 Member Level: Gold   Points : 0

    Hi Thai,

    Yes you should call the javascript for capture your current screen from your webcam.

    Regards,
    Damu

    Guest Author: sharad14 May 2012

    hi,
    i have used your code in my project it works with browser installed with adobe version 11.1.102.62 but it is not working properly with 11.2.202.235 it's not showing webcam on browser.

    Author: sharad14 May 2012 Member Level: Bronze   Points : 1

    i have used your code in my project it works with Mozilla where adobe installed with version 11.1.102.62 installed but it is not working with ie 8 and Google crome where latest version is installed i.e. 11.2.202.235. what to do?Is it adobe version issue or flash file issue?

    Guest Author: 15 May 2012

    Can anybody help in capturing Video and record to server

    Author: DAMU15 May 2012 Member Level: Gold   Points : 1

    Hi Shared,

    What code is not working in google chrome and other browsers? The flash file is not loaded properly or the web cam is not displayed properly.

    Please post your complete details about the browser issue.

    Guest Author: 15 May 2012

    Hey Damu, Could you pls help me video capturing

    Author: DAMU15 May 2012 Member Level: Gold   Points : 1

    Hi Hari,

    You can't use this same flash file for capturing a video from your web cam. If you want to do the same using flash, then discuss your idea to flash designer.

    Please refer the below site,
    http://www.codeproject.com/Articles/7637/DirectX-Video-Stream-and-frame-capture

    Author: sharad15 May 2012 Member Level: Bronze   Points : 1

    Hi damu,
    i also don't know what actually happening here,
    it works sometime in ie8. but not in crome.
    The flash file is loaded properly,i able to see capture button but not able to see webcam in browser

    Author: DAMU15 May 2012 Member Level: Gold   Points : 0

    Hi Shared,

    Okay. I think, then the problem is your flash add-on settings. Right click on the flash file and click the settings and select the option to enable the web cam.

    Author: sharad16 May 2012 Member Level: Bronze   Points : 1

    Hi damu,
    i solved this one.
    Actually problem is not with webcam or flash.
    It's because of if my application is running on Mozilla with webcam on,it wont available for ie8 or other browser at same time.
    Thanks for your help and time.

    Author: sharad16 May 2012 Member Level: Bronze   Points : 1

    Hi damu,
    i solved this one.
    Actually problem is not with webcam or flash.
    It's because of if my application is running on Mozilla with webcam on,it wont available for ie8 or other browser at same time.
    Thanks for your help and time.

    Guest Author: monalisha Panigrahi19 May 2012

    Hi,It working perfectly.But i am unable to store the image in a folder.I gave the the Path but still it is not saving the image in that folder.Can u plz help me.
    Thanks..............

    Guest Author: raja20 May 2012

    how can i save this image to my computer, and how can i edit fla file, please help me urjent

    Guest Author: habeeba02 Jun 2012

    how can i edit fla file

    Author: Kanwalpreet Singh09 Jun 2012 Member Level: Silver   Points : 1

    it is gud
    but i want to use this with the window application
    can i use same code for that.like i am creating window application in which i want to click a pic on the click of button.
    so please tell me how i cvan do that....

    Author: DAMU12 Jun 2012 Member Level: Gold   Points : 0

    Hi Kanwalpreet,

    You can't use the same source to windows application. Refer the below site,
    http://easywebcam.codeplex.com/

    Guest Author: Nanhey khan09 Jul 2012

    Hi Sir,

    It is working well.

    but i have a problem that i want to expand image size
    but it is not expanded.
    So please give some ideas to overcome this problem.
    Its urgent sir

    Author: DAMU19 Jul 2012 Member Level: Gold   Points : 0

    Hi Nanhey Khan,

    Do you need to change the size of captured image or you need to change the size of your webcam?

    Guest Author: Pavankumar22 Jul 2012

    Thanks for share the article, this article helped me a lot but I had one query i.e a message need to be given for user after clicking on capture button as "Image uploaded successfully.."

    Guest Author: Neha27 Jul 2012

    Hello Sir, I need your help.I want to save the captured image from webcam to the server folder.Please reply as this is urgent...
    Thank in advance!!!

    Guest Author: tenh30 Jul 2012

    i fellow you codes but when run nothing happen. so how to do?

    Guest Author: Manish07 Aug 2012

    Hello DAMU,
    Thanks for Posting. But I am facing a problem while capturing an image. Image is not saving on exact given path
    FileStream fs = new FileStream("C:\\Webcam.jpg", FileMode.OpenOrCreate, FileAccess.Write);

    Please tell me how i can store the Captured Image as JPG.
    PLZ PLZ PLZ.

    Author: DAMU07 Aug 2012 Member Level: Gold   Points : 1

    Hello Manish,

    I have reviewed your post and noticed you have tried to save the captured images in your c drive. Generally, c drive doesn't have the full access rights to create a new file so, you can try to save the capture images into some other location.

    Regards,
    Damu

    Guest Author: Nagarajan08 Aug 2012

    Hello DAMU,

    Thanking Very Much...
    Its very useful us..

    While capturing the image want to ask the save location window..
    its possible to create....

    Author: DAMU13 Aug 2012 Member Level: Gold   Points : 1

    Hello Nagarajan,

    Thank you for your feedback. It is possible to ask the save location window after capture the image but we need to modified that code in flash with the help of action script. So, you can request the same to your flash designers to do it.

    Guest Author: Manish Kumar14 Aug 2012

    Thanks a lot DAMU..

    Guest Author: Pardeep Kuashik15 Aug 2012

    Very Nice.....Perfect...

    Guest Author: ujjwal uniyal24 Aug 2012

    Really good article.. Can you please tell me how to Set this image as source of an image control on capture button click ?? I don't know how to do changes in action script to set this image as source for some control.

    Guest Author: ujjwal uniyal24 Aug 2012

    Really good article.. Can you please tell me how to Set this image as source of an image control on capture button click ?? I don't know how to do changes in action script to set this image as source for some control.

    Guest Author: Aakash24 Aug 2012

    Really good Article. Can you please tell me how to set this image as source of an image control on same page.

    Guest Author: Surendra14 Sep 2012

    Hi Damu,

    Thanks for your article.

    Can you please tell me how to change the form to another form in save_image.swf file and image length(320*240).
    Note: I have change the detais. but it is not working.

    Guest Author: Riya27 Sep 2012

    Nice Article DAMU !!

    I Have hosted application on iis 7.0 . When i am taking the image it is not getting saved in the folder on server however it works when i run it locally. Please suggest some solution

    Author: DAMU01 Oct 2012 Member Level: Gold   Points : 1

    Hi Riya,

    For save the pictures in your folder you must have the full access rights to access this folder. When you run it locally, it has full access rights so it has working fine. In IIS, you need to manually set the access rights to the folder. Right click on your folder and select the "Security" tab and give the permission to all type of users.

    Guest Author: Surendra07 Oct 2012

    Hi Damu,

    Thanks for your article.
    Can you please tell me how to change the form to another form in save_image.swf file and image length(320*240).
    Note: I have change the details. but it is not working.

    Guest Author: rahul13 Oct 2012

    Janu devach pawla...thanks a lot :)

    Guest Author: puneethg19 Oct 2012

    HI All,

    I am capturing image using web cam (which is connected to my pc) but in server the image is not saving. Please can anybody help me out for this solution

    Guest Author: priyabs28 Oct 2012

    Hi

    I want to validate whether the camera is availabe or not in clients device. could anyone tell me how to achieve this?

    Author: sunshine31 Oct 2012 Member Level: Bronze   Points : 0

    Hi DAMU,

    Why I fail to pass values into page "ImageConversions.aspx"? Can u help me... thanks :)

    Author: sunshine01 Nov 2012 Member Level: Bronze   Points : 1

    Hi DAMU,

    I have passed values into page "ImageConversions.aspx" successfully. But i still have another question. My question is how can i make the "Capture" button redirect to second "ImageConversions2.aspx"? I have made two (default.aspx and default2.aspx) and two (ImageConversions.aspx and ImageConversions2.aspx). So "default2.aspx" i want it will link to "ImageConversions2.aspx". TQ

    Guest Author: Gunadi12 Nov 2012

    Hi Damu,
    I need to pass a specific filename (instead of 'Webcam.jpg') and pass the file name from default.aspx. In other words, the filename is controlled from default.aspx. I want to add the filename with something like session ID.
    This is to make sure the filename belong to the user with his/her session. How to do that?
    Thanks so much.

    Guest Author: Nilesh02 Dec 2012

    Hi Damu,
    I am getting this error when I deploy application on server
    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at save_picture_fla::MainTimeline/frame1()

    Guest Author: Adam11 Dec 2012

    Hi Damu

    Thanks for sharing, this has got me started nicely on a piece of work i am doing.

    Author: sunshine13 Dec 2012 Member Level: Bronze   Points : 2

    When i take the image it is not getting saved in the folder on server however it works when i run it locally. Please suggest some solution. Do i need to install any flash player on my server. Pls help, urgent. TQ

    Guest Author: meir16 Jan 2013

    Hi.
    this is great code. thank you.
    I would like to ask you how can i see camera from
    other user while he can see my camera, like in real chat.
    Do you have any examples.
    thank you.
    Meir

    Guest Author: Rick Morayniss25 Jan 2013

    My application is not calling the 2nd form. My guess folder structure. How can I get the image(= Request.Form["imageData"]) inside the default.aspx.cs?

    Guest Author: yamuna singh03 Feb 2013

    Dear sir,

    I apply this post in my application. it work fine.but i want stop button in this.because camera is still on while i submit the button. and left image is not showing. only capture image shown in the box. Please help me.

    Author: DAMU09 Feb 2013 Member Level: Gold   Points : 2

    Hi Yamuna Singh,

    Thanks for using this code. You need to customize the code on that flash files. If you have any flash action script they can review the code easily and update the functionality which you need for your application.

    Thanks,
    Damu

    Guest Author: siya12 Feb 2013

    This post helped me a lot.but i want to know how to resize the image which is been captured.actually i want to increase the size of image.

    Guest Author: syful23 Feb 2013

    Hi
    I'm using it on a project, and when I run in development environment it works, but when I run from the server that does not get the image of the flash file. No error found. Does it require any special permission.

    Guest Author: raj29 Mar 2013

    HELLO .. can i get the asp.net (VB) sample please ?

    I tried converting it, It runs but the image is not saved even if i save it in another place aprt frm C:,
    Please help.

    Guest Author: shahab01 Apr 2013

    Hi,
    how can i chnage image size to 320 x 240 in swf file.
    Means can you please tell me the line no. I have done lot of change for image size but couldn't increase the size from 200x150 to 320x200.
    Please

    Guest Author: Ragu05 Jun 2013

    Dear Damu,

    Application works fine in locally. But if i access via IIS, image is not saving. What kind of folder permission should i provide. Please explain me in detail .

    Thanks
    Ragu

    Guest Author: vijaya11 Sep 2013

    Sir its very good experience to use this App and it solved my problem.but when I publish my code and run as application it do not save image on c://,what to do sir please tell me

    Guest Author: vijaya11 Sep 2013

    Sir i am getting problem after publishing the code ,it do not save image after publishing web App


    Author: Thuan Nguyen21 Jan 2014 Member Level: Bronze   Points : 0

    Where's attach file? Pls give me the attach file?

    Author: Thuan Nguyen21 Jan 2014 Member Level: Bronze   Points : 0

    Where's attach file? Pls give me the attach file?

    Guest Author: www.datanesia.com26 Jan 2014

    how save picture from webcam
    FileStream fs = new FileStream("C:\\Webcam.jpg", FileMode.OpenOrCreate, FileAccess.Write); not running , not save how its problem can help me.

    Author: DAMU21 Feb 2014 Member Level: Gold   Points : 2

    Hi Datanesia,

    Thanks for your queries. I am not able to resolve your problem without any detail. Please upload your complete solution file. So that, I can verify it and let you know the solution for it.

    Thanks,
    Damu

    Author: DAMU19 Mar 2014 Member Level: Gold   Points : 2

    Hi Gino,

    You can download the attachments in top of the "Attachments" link. For your reference, I have posted the link here,

    http://www.dotnetspider.com/attachments/Resources/38150-4106-WebcamCapture.rar

    Guest Author: Lyle25 Aug 2014

    I'm not downloading and spending hours on this if it doesn't work anymore

    Author: DAMU25 Aug 2014 Member Level: Gold   Points : 0

    Hi Lyle,

    Let me know what error are you face on that. I have verified this and it's working fine for me.

    Thanks,
    Damu

    Guest Author: Harish Mohanan03 Oct 2014

    Sir,

    Thank you for the post.
    I need something similar to this to be done on Windows Form application. To be precise I need video + audio recording in a windows form in any of the popular formats (wmv, avi, mp4...) I tried to use many open source codes that were available. Many had compactibility issues whereas many others they worked, but the video was either slow or fast. My application requires the video to be exactly in sync with the time. And about the functionality. I dont need too much of functionality. Just simple Start Recording and StopRecording.

    Are you aware of any good open source projects that would be of help to me. I am also ok with paid version, if the amount aint too much.
    Please please please help!

    Thanks and Smiles
    Harish

    Author: ananthakumar28 May 2015 Member Level: Silver   Points : 0

    Hi,

    i was not able to find the attached file from this post can u send me the resource file to the mail ananthkind@gmail.com



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: