Click here to Skip to main content
15,888,527 members
Articles / Programming Languages / C#
Article

Webcam Web Service

Rate me:
Please Sign up or sign in to vote.
4.64/5 (11 votes)
13 May 20022 min read 416.7K   13.6K   103   65
ASP.NET Web Service written in C# to grab a picture from a webcam

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


Written By
Team Leader
France France
I am an experienced Team Leader & Senior Solutions Architect with a passion for shipping high-quality products by empowering development team and culture toward an agile mindset. I bring technical vision and strategy, leading engineering teams to move product, processes and architecture forward.

Comments and Discussions

 
GeneralBlack image Pin
Luan Almeida V.7-Apr-05 17:29
Luan Almeida V.7-Apr-05 17:29 
GeneralRe: Black image Pin
umang551-Apr-06 18:10
umang551-Apr-06 18:10 
QuestionHow to register AxCamClient.dll? Pin
wang896431-Mar-05 19:05
wang896431-Mar-05 19:05 
AnswerRe: How to register AxCamClient.dll? Pin
rukesh12-Jul-05 6:58
rukesh12-Jul-05 6:58 
AnswerRe: How to register AxCamClient.dll? Pin
alivepjc28-Dec-05 11:39
alivepjc28-Dec-05 11:39 
QuestionMultiple webcam support? Pin
unclepaul15-Dec-04 18:24
unclepaul15-Dec-04 18:24 
AnswerRe: Multiple webcam support? Pin
Laurent Kempé16-Dec-04 4:31
Laurent Kempé16-Dec-04 4:31 
GeneralJUNK CODING AT IE Pin
kkyirui8-Dec-04 14:31
kkyirui8-Dec-04 14:31 
oh..i have tested recently with xp pro, vsnet2003 and logitech quickcam.

when i press invoke, with value 15, the below coding appear.

<base64binary xmlns="http://tempuri.org/">/9j/4AAQSkZJRgABAQEAYABgAAD/4QBZRXhpZgAASUkqAAgAAAABAGmHBAABAAAAGgAAAAAAAAAB AIaSAgApAAAAKAAAAEludGVsKFIpIEpQRUcgTGlicmFyeSwgdmVyc2lvbiAxLDUsNCwzNgAA/9sA QwAIBgYHBgUIBwcHCQkICgwUDQwLCwwZEhMPFB0aHx4dGhwcICQuJyAiLCMcHCg3KSwwMTQ0NB8n OT04MjwuMzQy/9sAQwEJCQkMCwwYDQ0YMiEcITIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy MjIyMjIyMjIyMjIyMjIyMjIyMjIy/8AAEQgA8AFAAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAA AAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQy gZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVm Z2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS 09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYH CAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1Lw FWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5 eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj 5OXm5+jp6vLz9PX29/j5+v/aAAwDAQACEQMRAD8A838P2+gX5a2vtP1Np4rS5uXmg1CONW8qKSUK EMDEZCBc7j1z7U+DQ7XXNIvr7RrO4tvstxbxOLy/iaNFdZyzvIUjVBmONRnHLYySwAytGv4tOu55 pVdlksbu3AQDO6W3kjXqRxucZ9s9aI76JPDV3prI5mnvra4VsDaFjjnU55znMq447Gv0CtCuqsnT k/s26rWT5vkl9xypq2o6PQNUkuLW3Fm6z3V41jFHIQjGdSgZCCQVIMiDnA59jh8XhvVprSxu0tR9

Anyone got idea why is this happen? How to solve it?>
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
baguilar14-Jun-04 9:40 
AnswerRe: Documentation about CamServer? Pin
hktang7-Jul-04 0:00
hktang7-Jul-04 0:00 
GeneralRe: Documentation about CamServer? Pin
baguilar7-Jul-04 9:03
baguilar7-Jul-04 9:03 
AnswerRe: Documentation about CamServer? Pin
k_catch9-Aug-10 5:32
k_catch9-Aug-10 5:32 
GeneralCOM tab Pin
grazie12-Jun-04 19:59
grazie12-Jun-04 19:59 
Generaljava webcam web service Pin
grazie9-Jun-04 22:50
grazie9-Jun-04 22:50 
Generalayuda Pin
5353015030-Mar-04 4:13
5353015030-Mar-04 4:13 
GeneralRe: ayuda [Help] Pin
Colin Angus Mackay30-Mar-04 4:42
Colin Angus Mackay30-Mar-04 4:42 
GeneralRe: ayuda Pin
rrferreira21-Dec-06 10:46
rrferreira21-Dec-06 10:46 
Generaldll Error ...please help!! Pin
alexhongkong19-Jan-04 18:06
alexhongkong19-Jan-04 18:06 
GeneralRe: dll Error ...please help!! Pin
reel11-Aug-05 5:39
reel11-Aug-05 5:39 
GeneralRe: dll Error ...please help!! Pin
daldal11-Jan-06 12:48
daldal11-Jan-06 12:48 
GeneralHelp Pin
marnicola5-Jan-04 18:37
marnicola5-Jan-04 18:37 
GeneralMultiples WebCams on one PC Pin
Teh Joo Peng1-Jan-04 21:56
Teh Joo Peng1-Jan-04 21:56 
GeneralRe: Multiples WebCams on one PC Pin
Laurent Kempé2-Jan-04 10:46
Laurent Kempé2-Jan-04 10:46 
GeneralRe: Multiples WebCams on one PC Pin
Teh Joo Peng4-Jan-04 19:47
Teh Joo Peng4-Jan-04 19:47 

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.