Click here to Skip to main content
Click here to Skip to main content

Webcam Web Service using Direct Internet Message Encapsulation (DIME)

, 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 using DIME specification
<!-- Download Links -->

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:

using DimeSoapExtension;
using Dime;
using System.Net;
And finally add the GrabFrame Web Method to the source code obtained by the wizard:
[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:

using DimeSoapExtension;
Add the [DimeExtension] attribute to the GrabFrame method:
[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:
<meta http-equiv="Refresh" content="20">
It permits to the web page to automatically reload each 20 seconds. Now edit the code:
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

About the Author

Laurent Kempé
Architect
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).

Follow on   Twitter

Comments and Discussions

 
Generalhi, Pinmembersafia jamal6-Nov-10 8:57 
GeneralRe: hi, Pinmembersys16111727-Jul-11 1:54 
GeneralRe: hi, Pinmembersys16111727-Jul-11 1:55 
GeneralRe: hi,me too,help!!!! Pinmembersys16111727-Jul-11 1:55 
GeneralReg camserver.dll PinmemberMalathi Raji14-Jul-10 21:03 
Questioncan i integrate web cam in my web page with this article Pinmemberponugantibabu18-Mar-09 0:34 
GeneralDisplay grabbed picture in a web page control Pinmembermfc_surfer3-Sep-07 23:14 
GeneralRe: Display grabbed picture in a web page control PinmemberLaurent Kempé4-Sep-07 1:34 
QuestionCannot implicity convert type Pinmembershiva123414-May-07 0:30 
GeneralRe: Cannot implicity convert type Pinmembervenkeeyy14-Apr-08 23:13 
QuestionSame prob. i.e regarding attachment Pinmemberjackandersoninvalley4-Feb-07 0:19 
GeneralProblem in Image PinmemberAnandhveera24-Jan-07 23:39 
Generaltrial does not work Pinmemberjitender _t16-May-06 19:58 
GeneralProblem with my code [modified] Pinmembermehdi600218-Oct-05 5:46 
GeneralSorry,I cann't download the server soruce. PinsussJasonShen16-Oct-05 20:21 
GeneralRe: Sorry,I cann't download the server soruce. PinmemberLaurent Kempé16-Oct-05 21:14 
GeneralBlack Image PinsussJuan_Paulo26-Aug-04 15:08 
GeneralRe: Black Image Pinmembermohamed seif24-Jun-05 5:48 
GeneralRe: Black Image Pinmemberamit.andharia@etatvasoft.com2-Sep-06 1:51 
GeneralSetting up image server.... Pinmemberghatti24-Nov-03 23:32 
GeneralSending Web Cam Image from Client to Web Service PinmemberPrash23103-Nov-03 17:36 
GeneralError in client program PinsussAnonymous30-Sep-03 21:32 
GeneralDownload error Pinmembermmullai@yahoo.7-May-03 4:57 
GeneralRe: Download error PinmemberLaurent Kempé7-May-03 11:36 
GeneralCannot add reference "CamServer.dll" Pinmemberisaac126-Jan-03 2:05 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 14 May 2002
Article Copyright 2002 by Laurent Kempé
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid