Click here to Skip to main content
13,452,396 members (55,042 online)
Click here to Skip to main content
Add your own
alternative version


57 bookmarked
Posted 13 May 2002

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 -->



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

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:
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;

        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() + 
                     " - " + 

    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 ),

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

    //Build DimeAttachement containing picture
    DimeAttachment attachment = new DimeAttachment( "image/jpeg", 
                                                    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:
public DimeAttachment GrabFrame(short nQuality) 
Next remove the generated DimeAttachement class from the proxy. We want the
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 );


    //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.


You may try it here: See Me


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.


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

Generalhi, Pin
safia jamal6-Nov-10 8:57
membersafia jamal6-Nov-10 8:57 
GeneralRe: hi, Pin
sys16111727-Jul-11 1:54
membersys16111727-Jul-11 1:54 
GeneralRe: hi, Pin
sys16111727-Jul-11 1:55
membersys16111727-Jul-11 1:55 
GeneralRe: hi,me too,help!!!! Pin
sys16111727-Jul-11 1:55
membersys16111727-Jul-11 1:55 
GeneralReg camserver.dll Pin
Malathi Raji14-Jul-10 21:03
memberMalathi Raji14-Jul-10 21:03 
Questioncan i integrate web cam in my web page with this article Pin
ponugantibabu18-Mar-09 0:34
memberponugantibabu18-Mar-09 0:34 
GeneralDisplay grabbed picture in a web page control Pin
mfc_surfer3-Sep-07 23:14
membermfc_surfer3-Sep-07 23:14 
GeneralRe: Display grabbed picture in a web page control Pin
Laurent Kempé4-Sep-07 1:34
memberLaurent Kempé4-Sep-07 1:34 
QuestionCannot implicity convert type Pin
shiva123414-May-07 0:30
membershiva123414-May-07 0:30 
GeneralRe: Cannot implicity convert type Pin
venkeeyy14-Apr-08 23:13
membervenkeeyy14-Apr-08 23:13 
QuestionSame prob. i.e regarding attachment Pin
jackandersoninvalley4-Feb-07 0:19
memberjackandersoninvalley4-Feb-07 0:19 
GeneralProblem in Image Pin
Anandhveera24-Jan-07 23:39
memberAnandhveera24-Jan-07 23:39 
Generaltrial does not work Pin
jitender _t16-May-06 19:58
memberjitender _t16-May-06 19:58 
GeneralProblem with my code [modified] Pin
mehdi600218-Oct-05 5:46
membermehdi600218-Oct-05 5:46 
GeneralSorry,I cann't download the server soruce. Pin
JasonShen16-Oct-05 20:21
sussJasonShen16-Oct-05 20:21 
GeneralRe: Sorry,I cann't download the server soruce. Pin
Laurent Kempé16-Oct-05 21:14
memberLaurent Kempé16-Oct-05 21:14 
GeneralBlack Image Pin
Juan_Paulo26-Aug-04 15:08
sussJuan_Paulo26-Aug-04 15:08 
GeneralRe: Black Image Pin
mohamed seif24-Jun-05 5:48
membermohamed seif24-Jun-05 5:48 
GeneralRe: Black Image Pin
amit.andharia@etatvasoft.com2-Sep-06 1:51
memberamit.andharia@etatvasoft.com2-Sep-06 1:51 
GeneralSetting up image server.... Pin
ghatti24-Nov-03 23:32
memberghatti24-Nov-03 23:32 
GeneralSending Web Cam Image from Client to Web Service Pin
Prash23103-Nov-03 17:36
memberPrash23103-Nov-03 17:36 
GeneralError in client program Pin
Anonymous30-Sep-03 21:32
sussAnonymous30-Sep-03 21:32 
GeneralDownload error Pin
mmullai@yahoo.7-May-03 4:57
membermmullai@yahoo.7-May-03 4:57 
GeneralRe: Download error Pin
Laurent Kempé7-May-03 11:36
memberLaurent Kempé7-May-03 11:36 
GeneralCannot add reference "CamServer.dll" Pin
isaac126-Jan-03 2:05
memberisaac126-Jan-03 2:05 

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
Web02 | 2.8.180318.3 | Last Updated 14 May 2002
Article Copyright 2002 by Laurent Kempé
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid