![]() |
Languages »
C / C++ Language »
General
Intermediate
Webcam Web ServiceBy Laurent KempéASP.NET Web Service written in C# to grab a picture from a webcam |
C#, Windows, .NET1.0, Visual-Studio, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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.
[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(); }
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.
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.
| Version 1.00 |
April 10, 2002
First release. |
General
News
Question
Answer
Joke
Rant
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 |