Click here to Skip to main content
Licence 
First Posted 24 Jun 2002
Views 360,973
Bookmarked 119 times

Dynamic Webcam Image

By | 24 Jun 2002 | Article
Streaming Dynamic Images from your webcam into your web pages.

Contents

Introduction

This project presents the way to dynamically generate a picture with C# and ASP.NET. Basically a browser points to your page and gets back a picture grabbed on your webcam.

You will be able to access your webcam by placing a HTML IMG tag on your webpage. The picture at the top is an example of usage.

To be able to use the webcam you need to register with regsvr32 the COM Dll CamServer.dll in the 'COM Component' directory.

Method

The first thing to do to get a picture from an ASP.NET page is to determine the MIME file type that the file will handle. In our case the MIME type is JPEG. So we define in the ContentType of the Page:

<%@ Page Language="c#" ContentType="image/jpeg" %>
We also need to specify at the top of the page the namespace that will be used:
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Net" %>
Then comes the code that will grab the image from the wbecam and returns it to the client:
<script language="C#" runat="server">

protected void Page_Load(object sender, EventArgs e)
{
    //Jpeg compression quality
    short nQuality = 45;

    //Shout a picture from my webcam
    CAMSERVERLib.Camera cam = new CAMSERVERLib.CameraClass();

    byte[] picture = (byte[])cam.GrabFrame( nQuality );

    //Add the hour to the jpeg picture
    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, 12 ),
                    new SolidBrush( Color.Black ), 
                    new RectangleF( 1,1,320,240 ),
                    drawFormat
                );

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

    //Get codecs
    ImageCodecInfo[] icf = ImageCodecInfo.GetImageEncoders();

    EncoderParameters encps = new EncoderParameters( 1 );
    EncoderParameter encp = new EncoderParameter( System.Drawing.Imaging.Encoder.Quality, 
                                                  (long) nQuality );

    //Set quality
    encps.Param[0] = encp;

    bmp.Save( Response.OutputStream, icf[1], encps );
    
    g.Dispose();
    bmp.Dispose();
}
</script>
In the code there is nothing new, compared to the other articles. We get a jpeg picture grabbed by the webcam using COM Interop. We add the hour and the little difference is that the image stream is saved to Response.OutputStream .

Usage

You may reference the ASP.NET page has a normal jpeg picture:

<img src="http://XXX/WebcamPicture/WebForm1.aspx" width="320" height="240"/>
or in a ASP:Image control.

Conclusion

This opens lots of possibilities in the creation of dynamic graphics. For example you may create dynamic charts linked to database values returned has a normal graphic in a presentation page. You may also make easily a visit counter for your pages...

Known Issues

  • None.

History

Version 1.00 June 14, 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

Member

Follow on Twitter Follow on Twitter

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



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralStoring in database PinmemberMember 335582021:17 9 Apr '09  
General[My vote of 1] hi need help Pinmemberfiras_maher22:58 8 Apr '09  
QuestionWebcam: Registering CamServer.dll PinmemberTushar.Johri12:55 14 Nov '08  
Questionstoring the images Pinmemberjajjssddd21:12 23 Sep '08  
GeneralThanks PinmemberFastfootskater6:55 12 Jul '08  
GeneralPanda Software alert of virus on the project Pinmembernick7118:31 6 May '08  
GeneralRe: Panda Software alert of virus on the project PinmemberLaurent Kempé22:27 6 May '08  
Strange!
The full source code of this is in the other article if I remember correctly (so long time ago):
http://www.codeproject.com/KB/webservices/webcam.aspx[^]
 
Best Regards,
 
Laurent Kempé [Microsoft ASP.NET MVP]
Visit my web site Tech Head Brothers.
 
---
Old programmers never die, they just branch to a new address.

Generalregister the dll Pinmemberriccardodaria5:43 28 Apr '08  
Questionhow to this capture image in database Pinmembermastermehta21:00 26 Feb '08  
Generalneed capture the photo in client system Pinmemberarumguavel19:36 24 Feb '08  
GeneralRe: need capture the photo in client system Pinmembervenkeeyy21:26 14 Apr '08  
Generalwant to check web cam connect or not Pinmemberarumguavel18:07 30 Jan '08  
Generalhelp Pinmemberizzettamer1:14 2 Oct '07  
GeneralRe: help Pinmemberdaldal7:35 13 Oct '07  
GeneralERROR GrabFrame(). PinmemberHariom narayan1:21 20 Jul '07  
GeneralRe: ERROR GrabFrame(). Pinmembershamaaaaaaaaaaaaaa2:30 26 Apr '08  
GeneralRe: ERROR GrabFrame(). PinmemberMember 575127521:46 3 Dec '08  
GeneralRe: ERROR GrabFrame(). Pinmemberjonno0073:45 16 Nov '09  
GeneralRe: ERROR GrabFrame(). Pinmember~~BAKKAR~~2:31 23 Oct '08  
GeneralRe: ERROR GrabFrame().WebDev.WebServer.exe[1348] Pinmemberajau.jain23:13 17 Feb '09  
GeneralRe: ERROR GrabFrame(). Pinmemberk_catch6:32 5 Aug '10  
GeneralThanks! PinmemberRonald Colijn23:43 17 Jun '07  
GeneralRe: Thanks! Pinmembersumantmunna20:21 24 Jun '07  
Generalerror Pinmemberizzettamer2:23 1 Oct '07  
Generalblack image with date & time Pinmembershiva12340:34 14 May '07  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 25 Jun 2002
Article Copyright 2002 by Laurent Kempé
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid