Click here to Skip to main content
13,197,796 members (40,504 online)
Click here to Skip to main content
Add your own
alternative version


103 bookmarked
Posted 13 May 2002

Webcam Web Service

, 13 May 2002
Rate this:
Please Sign up or sign in to vote.
ASP.NET Web Service written in C# to grab a picture from a webcam
<!-- Download Links -->



C# Webcam is a port to ASP.NET and C# of the C++ ATL Web Service. It demonstrates the ease of use of C# and ASP.NET to build Web Services compared to ATL Server Web Service.

The project is still based on the Webcam COM component developed in the C++ ATL Web Service. .NET COM Interop is used to import it in the C# project.

Web Service

With Visual Studio, create a new C# ASP.NET Web Service project. Add a reference to 'CamServer 1.0 Type Library' on the COM tab. If 'CamServer 1.0 Type Library' is not in the list, you need to register the Dll CamServer.dll in the 'COM Components' directory. Add this Web Method to the source code obtained by the wizard:

public byte[] GrabFrame( short nQuality )
    //Shoot a picture from my webcam
    CAMSERVERLib.Camera cam = new CAMSERVERLib.CameraClass();

    return (byte[])cam.GrabFrame( nQuality );
Compared to the C++ ATL Web Service we need only three lines of code to get the picture from the COM component and return it to the client.

To have a little more fun coding I decided to add some features. Now it writes to the Windows EventLog the IP and the name of the machine calling it, after a DNS resolution. I have also added the date and hour of the local computer directly in the picture. And at last it increments a system performance counter to keep an eye on the number of people calling it.
public byte[] GrabFrame( short nQuality )
    //Write to the EventLog the IP and Host name
    string addr = HttpContext.Current.Request.UserHostAddress.Trim();

    string name;

    IPHostEntry host = null;

        host = System.Net.Dns.GetHostByAddress( addr );

        if ( host != null )
            name = host.HostName;
            for( int i = 0; i < host.Aliases.Length; i++ )
                name += "*" + host.Aliases[i];

            eventLog.WriteEntry( addr + " : " + name );
            name = "ERROR";

            eventLog.WriteEntry( name );
    catch( System.Exception error )
        // process the error error.Message

    //Shoot a picture from my webcam
    CAMSERVERLib.Camera cam = new CAMSERVERLib.CameraClass();

    byte[] picture = (byte[])cam.GrabFrame( nQuality );

    //Increments Performance Counter

    //Add the hour
    MemoryStream ms = new MemoryStream( picture );
    Bitmap bmp = new Bitmap( ms );

    Graphics g = Graphics.FromImage( bmp );

    string strDate = DateTime.Now.ToLongDateString() + 
                     " - " + 

    g.DrawString(   strDate,
                    new Font( FontFamily.GenericSansSerif, 10 ),
                    new SolidBrush( Color.Black ), 
                    new RectangleF( 1,1,320,240 ) 

    g.DrawString(   strDate,
                    new Font( FontFamily.GenericSansSerif, 10 ),
                    new SolidBrush( Color.White ), 
                    new RectangleF( 0,0,320,240 ) 

    MemoryStream ms2 = new MemoryStream();

    //Get codecs
    ImageCodecInfo[] icf = ImageCodecInfo.GetImageEncoders();

    EncoderParameters encps = new EncoderParameters( 1 );
    EncoderParameter encp = new EncoderParameter( Encoder.Quality, (long) nQuality );

    //Set quality
    encps.Param[0] = encp;

    bmp.Save( ms2, icf[1], encps );

    return ms2.ToArray();


The client ActiveX has no need to be modified. It is just needed to change the url of the Web Service, that is now:

Don't forget to register with regsvr32.exe the AxCamClient.dll in the 'COM Components' directory.


With few lines of source code you may obtain the same result then with little more C++ work.

The solution may be better because the COM component pack the picture, we unpack it to add date and hour then we pack again the new picture to send back to the client.

Problems Faced 

  • None.


Version 1.00 April 10, 2002
First release.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Laurent Kempé
France France

Laurent Kempé is the editor, founder, and primary contributor of Tech Head Brothers, a French portal about Microsoft .NET technologies.

He is currently employed by Innoveo Solutions since 10/2007 as a Senior Solution Architect and certified Scrum Master.

Founder, owner and Managing Partner of Jobping, which provides a unique and efficient platform for connecting Microsoft skilled job seekers with employers using Microsoft technologies.

Laurent is awarded by Microsoft since Avril 2002: Most Valuable Professional (MVP).

You may also be interested in...


Comments and Discussions

QuestionNot able to download the code Pin
Member 103126722-Oct-13 23:00
memberMember 103126722-Oct-13 23:00 
BugClass not registered error Pin
ravigpandey5-Aug-13 12:18
memberravigpandey5-Aug-13 12:18 
GeneralMy vote of 5 Pin
Ashish Rathod14-Dec-12 2:03
memberAshish Rathod14-Dec-12 2:03 
Question[Ask] why after i register camserver.dll i still cannot find CamServer 1.0 Type Library on COM tab? Pin
oqiee piphi poett2-Nov-11 16:48
memberoqiee piphi poett2-Nov-11 16:48 
QuestionGrabFrame error Pin
international ninja28-Sep-11 11:16
memberinternational ninja28-Sep-11 11:16 
Generalhi guys Pin
amiyamohanta29-Jun-10 8:52
memberamiyamohanta29-Jun-10 8:52 
QuestionVideo in real-time (stream) Pin
lgallogarcia26-Sep-09 5:17
memberlgallogarcia26-Sep-09 5:17 
AnswerRe: Video in real-time (stream) Pin
anuradha venkatesham2-Aug-12 23:04
memberanuradha venkatesham2-Aug-12 23:04 
i am not able to register the dll , kindly help
GeneralUnable to select an webcam Pin
Wcverfrisser23-Jul-08 23:25
memberWcverfrisser23-Jul-08 23:25 
QuestionHow to do with two usb webcams ??? Pin
danieln219-Jun-08 13:41
memberdanieln219-Jun-08 13:41 
GeneralI Am New to USe Com Component Help Me Pin
123sandip5-Jun-08 19:20
member123sandip5-Jun-08 19:20 
Generalyou have virus Pin
chemita_cpu30-Aug-07 22:20
memberchemita_cpu30-Aug-07 22:20 
GeneralAny idea using a pocket pc as webcam Pin
startstarts6-Jun-07 22:58
memberstartstarts6-Jun-07 22:58 
Generalcannot register CamServer.dll Pin
joejohnholdsworth9-Mar-07 1:34
memberjoejohnholdsworth9-Mar-07 1:34 
GeneralRe: cannot register CamServer.dll Pin
joejohnholdsworth9-Mar-07 1:44
memberjoejohnholdsworth9-Mar-07 1:44 
GeneralRe: cannot register CamServer.dll Pin
danieln219-Jun-08 13:37
memberdanieln219-Jun-08 13:37 
Generalfailure in running webservice service1 Pin
rinal bhogi21-Mar-06 10:24
memberrinal bhogi21-Mar-06 10:24 
GeneralReload image Pin
frankjonathan816-Feb-06 9:56
memberfrankjonathan816-Feb-06 9:56 
Generalerror grab frame Pin
fcyjk,kl;25-Aug-05 15:09
memberfcyjk,kl;25-Aug-05 15:09 
QuestionWhy is it so slow to grab am image and show it? Pin
davezh24-Jul-05 18:05
memberdavezh24-Jul-05 18:05 
AnswerRe: Why is it so slow to grab am image and show it? Pin
k_catch6-Aug-10 3:55
memberk_catch6-Aug-10 3:55 
GeneralBlack image Pin
Luan Almeida V.7-Apr-05 17:29
memberLuan Almeida V.7-Apr-05 17:29 
GeneralRe: Black image Pin
umang551-Apr-06 18:10
memberumang551-Apr-06 18:10 
QuestionHow to register AxCamClient.dll? Pin
wang896431-Mar-05 19:05
memberwang896431-Mar-05 19:05 
AnswerRe: How to register AxCamClient.dll? Pin
rukesh12-Jul-05 6:58
memberrukesh12-Jul-05 6:58 
AnswerRe: How to register AxCamClient.dll? Pin
alivepjc28-Dec-05 11:39
memberalivepjc28-Dec-05 11:39 
QuestionMultiple webcam support? Pin
ucndlldfdfdf15-Dec-04 18:24
memberucndlldfdfdf15-Dec-04 18:24 
AnswerRe: Multiple webcam support? Pin
Laurent Kempé16-Dec-04 4:31
memberLaurent Kempé16-Dec-04 4:31 
kkyirui8-Dec-04 14:31
memberkkyirui8-Dec-04 14:31 
QuestionHello!! Can i ask something? Pin
Koreanboy20-Sep-04 9:14
sussKoreanboy20-Sep-04 9:14 
QuestionDocumentation about CamServer? Pin
baguilar14-Jun-04 9:40
memberbaguilar14-Jun-04 9:40 
AnswerRe: Documentation about CamServer? Pin
hktang7-Jul-04 0:00
memberhktang7-Jul-04 0:00 
GeneralRe: Documentation about CamServer? Pin
baguilar7-Jul-04 9:03
memberbaguilar7-Jul-04 9:03 
AnswerRe: Documentation about CamServer? Pin
k_catch9-Aug-10 5:32
memberk_catch9-Aug-10 5:32 
GeneralCOM tab Pin
grazie12-Jun-04 19:59
membergrazie12-Jun-04 19:59 
Generaljava webcam web service Pin
grazie9-Jun-04 22:50
membergrazie9-Jun-04 22:50 
Generalayuda Pin
Lnacv30-Mar-04 4:13
sussLnacv30-Mar-04 4:13 
GeneralRe: ayuda [Help] Pin
Colin Angus Mackay30-Mar-04 4:42
memberColin Angus Mackay30-Mar-04 4:42 
GeneralRe: ayuda Pin
rrferreira21-Dec-06 10:46
memberrrferreira21-Dec-06 10:46 
Generaldll Error ...please help!! Pin
alexhongkong19-Jan-04 18:06
memberalexhongkong19-Jan-04 18:06 
GeneralRe: dll Error ...please help!! Pin
reel11-Aug-05 5:39
memberreel11-Aug-05 5:39 
GeneralRe: dll Error ...please help!! Pin
daldal11-Jan-06 12:48
memberdaldal11-Jan-06 12:48 
GeneralHelp Pin
marnicola5-Jan-04 18:37
membermarnicola5-Jan-04 18:37 
GeneralMultiples WebCams on one PC Pin
Teh Joo Peng1-Jan-04 21:56
memberTeh Joo Peng1-Jan-04 21:56 
GeneralRe: Multiples WebCams on one PC Pin
Laurent Kempé2-Jan-04 10:46
memberLaurent Kempé2-Jan-04 10:46 
GeneralRe: Multiples WebCams on one PC Pin
Teh Joo Peng4-Jan-04 19:47
memberTeh Joo Peng4-Jan-04 19:47 
GeneralRe: Multiples WebCams on one PC Pin
Laurent Kempé4-Jan-04 20:32
memberLaurent Kempé4-Jan-04 20:32 
GeneralWhy not use Alt+Print Screen Pin
Osrald26-Dec-03 21:31
memberOsrald26-Dec-03 21:31 
GeneralRe: Why not use Alt+Print Screen Pin
Laurent Kempé26-Dec-03 21:55
memberLaurent Kempé26-Dec-03 21:55 
QuestionDoesn't work on Win2003 ?? Pin
marin.mamic@netlab.hr24-Nov-03 3:36
membermarin.mamic@netlab.hr24-Nov-03 3:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171020.1 | Last Updated 14 May 2002
Article Copyright 2002 by Laurent Kempé
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid