Click here to Skip to main content
6,822,613 members and growing! (20,671 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Intermediate

Webcam Web Service

By Laurent Kempé

ASP.NET Web Service written in C# to grab a picture from a webcam
C#, Windows, .NET1.0, Visual-Studio, Dev
Posted:13 May 2002
Views:211,844
Bookmarked:81 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 4.00 Rating: 4.00 out of 5
1 vote, 12.5%
1

2
1 vote, 12.5%
3
2 votes, 25.0%
4
4 votes, 50.0%
5

Contents

Introduction

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:

[WebMethod]
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.
[WebMethod]
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;

    try
    {
        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 );
        }
        else
        {
            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

    performanceCounterShoot.Increment();

    //Add the hour

    MemoryStream ms = new MemoryStream( picture );
    Bitmap bmp = new Bitmap( ms );

    Graphics g = Graphics.FromImage( bmp );

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

    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 );
    ms2.Close();

    return ms2.ToArray();
}

Client

The client ActiveX has no need to be modified. It is just needed to change the url of the Web Service, that is now:
http://YOUR_IP/webcamcsharp/service1.asmx?WSDL

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

Conclusion

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.

History

Version 1.00 April 10, 2002
First release.

License

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é


Member


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

Laurent is awarded by Microsoft since Avril 2002: Most Valuable Professional (MVP). You might contact him through his Blog.



Occupation: Web Developer
Location: France France

Other popular C / C++ Language articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 55 (Total in Forum: 55) (Refresh)FirstPrevNext
QuestionVideo in real-time (stream) Pinmemberlgallogarcia6:17 26 Sep '09  
GeneralUnable to select an webcam PinmemberWcverfrisser0:25 24 Jul '08  
GeneralHow to do with two usb webcams ??? Pinmemberdanieln2114:41 9 Jun '08  
GeneralI Am New to USe Com Component Help Me Pinmember123sandip20:20 5 Jun '08  
Generalyou have virus Pinmemberchemita_cpu23:20 30 Aug '07  
GeneralAny idea using a pocket pc as webcam Pinmemberstartstarts23:58 6 Jun '07  
Generalcannot register CamServer.dll Pinmemberjoejohnholdsworth2:34 9 Mar '07  
GeneralRe: cannot register CamServer.dll Pinmemberjoejohnholdsworth2:44 9 Mar '07  
GeneralRe: cannot register CamServer.dll Pinmemberdanieln2114:37 9 Jun '08  
Generalfailure in running webservice service1 Pinmemberrinal bhogi11:24 21 Mar '06  
GeneralReload image Pinmemberfrankjonathan8110:56 6 Feb '06  
Generalerror grab frame Pinmemberfcyjk,kl;16:09 25 Aug '05  
GeneralWhy is it so slow to grab am image and show it? Pinmemberdavezh19:05 24 Jul '05  
GeneralBlack image PinmemberLuan Almeida V.18:29 7 Apr '05  
GeneralRe: Black image Pinmemberumang5519:10 1 Apr '06  
GeneralHow to register AxCamClient.dll? Pinmemberwang896420:05 31 Mar '05  
GeneralRe: How to register AxCamClient.dll? Pinmemberrukesh7:58 12 Jul '05  
GeneralRe: How to register AxCamClient.dll? Pinmemberalivepjc12:39 28 Dec '05  
GeneralMultiple webcam support? Pinmemberucndlldfdfdf19:24 15 Dec '04  
GeneralRe: Multiple webcam support? PinmemberLaurent Kempé5:31 16 Dec '04  
GeneralJUNK CODING AT IE Pinmemberkkyirui15:31 8 Dec '04  
GeneralHello!! Can i ask something? PinsussKoreanboy10:14 20 Sep '04  
GeneralDocumentation about CamServer? Pinmemberbaguilar10:40 14 Jun '04  
GeneralRe: Documentation about CamServer? Pinmemberhktang1:00 7 Jul '04  
GeneralRe: Documentation about CamServer? Pinmemberbaguilar10:03 7 Jul '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 13 May 2002
Editor: Chris Maunder
Copyright 2002 by Laurent Kempé
Everything else Copyright © CodeProject, 1999-2010
Web21 | Advertise on the Code Project