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

Webcam Web Service using Direct Internet Message Encapsulation (DIME)

Rate me:
Please Sign up or sign in to vote.
4.00/5 (9 votes)
13 May 20023 min read 167.4K   1.5K   57   34
ASP.NET Web Service written in C# to grab a picture from a webcam using DIME specification

Contents

Introduction

This project presents a new approach of sending binary data through the wire using Direct Internet Message Encapsulation. You may download the 'DIME and

DimeSoapExtension
Sample' on Microsoft MSDN web site. During my experimentations of web service implementation I was always unhappy that binary datas had to be base64 encoded. With DIME you may send your binary data in its native format instead of requiring it to be encoded into XML using base-64 encoding, XML-escaped, or character set transformations into UTF-8. This is perfect to send to the client the jpeg picture grabbed on the webcam.

Web Service

This project will get the jpeg picture from the webcam using COM Interop, use GDI+ to add the time to the picture, use a DNS resolution to add the client's machine name to the Event Log and increments a system counter.

You may use as a starting point the solution DimeSample downloaded from MSDN. Add a new C# ASP.NET Web Service project called: DimeWebcamService. At this point you need to add references to projects Dime and DimeSoapExtension from the DimeSample Solution. Then 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 the using statements:

C#
using DimeSoapExtension;
using Dime;
using System.Net;
And finally add the GrabFrame Web Method to the source code obtained by the wizard:
C#
[DimeExtension]
[WebMethod]
public DimeAttachment 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(); 

    StringFormat drawFormat = new StringFormat();
    drawFormat.Alignment = StringAlignment.Center;

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

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

    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.Seek( 0, SeekOrigin.Begin );

    //Build DimeAttachement containing picture
    DimeAttachment attachment = new DimeAttachment( "image/jpeg", 
                                                    TypeFormatEnum.MediaType, 
                                                    ms2 );
    return attachment;
}
To be able to use the DimeAttachement class as a return value of the Web Service we need to add the attribute [DimeExtension] to the method GrabFrame. Now that we have the Web Service delivering the jpeg picture we need to display it on a client.

ASP.NET Client

In past articles the client was implemented as an ActiveX. It was good for the exercise but had drawbacks, for example you had to install the ActiveX on the client machine.

This time the client is implemented has an ASP.NET Web Application. Add a new C# ASP.NET Web Application to DimeSample solution, call it DimeASPNETClient.

Again you need to add references to projects Dime and DimeSoapExtension from the DimeSample Solution. Then add a web reference to http://localhost/DimeSample/DimeWebcamService/DimeWebcamService.asmx. Rename created localhost folder to TechHead. By adding a web reference we created a proxy to access the DIME Webcam Web Service. Now we need to modify it. In the Solution Explorer window make sure the DimeASPNETClient is selected. Click on "Show All files" icon. Expand the "Web References" node in the DimeASPNETClient project until you see Reference.cs. Reference.cs is the generated proxy. Double click Reference.cs toedit it in the code editor.

Add the using statement:

C#
using DimeSoapExtension;
Add the [DimeExtension] attribute to the GrabFrame method:
C#
[DimeExtension]
public DimeAttachment GrabFrame(short nQuality) 
{
Next remove the generated DimeAttachement class from the proxy. We want the
DimeAttachement
parameters to use DimeSoapExtension.DimeAttachment instead.

We have everything to be able to call the DIME Webcam Web Service. We then need to display the picture returned. Double click on the file Webform1.aspx to run the editor. Using the toolbox add an image to the page with a width of '320px' and height of '240px'. Edit the HTML and add in the meta part:
C#
<meta http-equiv="Refresh" content="20">
It permits to the web page to automatically reload each 20 seconds. Now edit the code:
C#
private void Page_Load(object sender, System.EventArgs e)
{
    //Grab a picture from the Web Service
    TechHead.Service1 wc = new TechHead.Service1();
    
    DimeAttachment pic = wc.GrabFrame( 65 );

    //Save the picture on the server
    FileStream fs = new FileStream( Server.MapPath( "webcam.jpg" ), 
                                    FileMode.Create, FileAccess.Write );

    byte[] fsPic = new byte[pic.Stream.Length];

    pic.Stream.Read(fsPic, 0, (int)pic.Stream.Length);
    
    fs.Write( fsPic, 0, fsPic.Length );

    fs.Close();

    //Change Image URL
    Image1.ImageUrl = Server.UrlPathEncode( "webcam.jpg" );
}
The page is reloaded due to the Refresh meta, so the Page_Load method is called each 20 seconds.

Trial

You may try it here: See Me

Conclusion

As a conclusion I made some evaluation of the DIME and Base64 ways of getting binary data from a Web Service. I used a proxy to trace outputs of the Web Service. The results are based on ten GrabFrame: Base64 = 184 658 bytes and DIME = 140 660 bytes. The difference is around 30 percents. You also have to consider that it is not needed for the server to encode and for the client to decode with the DIME solution.

Problems Faced 

  • None.

History

Version 1.00 May 13, 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

 
GeneralError in client program Pin
Anonymous30-Sep-03 21:32
Anonymous30-Sep-03 21:32 
GeneralDownload error Pin
Member 3827527-May-03 4:57
Member 3827527-May-03 4:57 
GeneralRe: Download error Pin
Laurent Kempé7-May-03 11:36
Laurent Kempé7-May-03 11:36 
GeneralCannot add reference "CamServer.dll" Pin
isaac126-Jan-03 2:05
isaac126-Jan-03 2:05 
GeneralRe: Cannot add reference "CamServer.dll" Pin
Laurent Kempé26-Jan-03 6:14
Laurent Kempé26-Jan-03 6:14 
GeneralRe: Cannot add reference "CamServer.dll" Pin
isaac129-Jan-03 4:40
isaac129-Jan-03 4:40 
GeneralRe: Cannot add reference "CamServer.dll" Pin
Laurent Kempé31-Jan-03 10:16
Laurent Kempé31-Jan-03 10:16 
GeneralRe: Cannot add reference &quot;CamServer.dll&quot; Pin
okoji Cyril22-Feb-05 21:29
okoji Cyril22-Feb-05 21:29 
goto where u have ur system32 and do this depending on the drive ur os is installed.

ie C:\winnt\system3\regsvr32 CamServer.dll


Magic has often been thought of us the art of making dreams come true; the art of realizing visions. Yet before we can bring birth to the vision we have to see it.

QuestionIs this code camera specific? Pin
Randy Kreisel30-Oct-02 10:48
Randy Kreisel30-Oct-02 10:48 
AnswerRe: Is this code camera specific? Pin
Laurent Kempé31-Oct-02 21:13
Laurent Kempé31-Oct-02 21:13 
AnswerRe: Is this code camera specific? Pin
Member 269557930-Apr-09 10:34
Member 269557930-Apr-09 10:34 
GeneralRe: Is this code camera specific? Pin
Randy Kreisel30-Apr-09 11:07
Randy Kreisel30-Apr-09 11:07 
GeneralNice work ! Pin
Ranjeet Chakraborty6-Sep-02 9:48
Ranjeet Chakraborty6-Sep-02 9:48 

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.