Click here to Skip to main content
15,867,330 members
Articles / Web Development / ASP.NET
Article

Dynamic Webcam Image

Rate me:
Please Sign up or sign in to vote.
4.65/5 (19 votes)
24 Jun 20021 min read 552.1K   18.3K   126   123
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:
C#
<%@ 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:
C#
<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:

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


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 Pin
izzettamer1-Oct-07 2:23
izzettamer1-Oct-07 2:23 
Generalblack image with date & time Pin
shiva123414-May-07 0:34
shiva123414-May-07 0:34 
GeneralRe: black image with date & time Pin
pask6-Jun-07 20:13
pask6-Jun-07 20:13 
GeneralRe: black image with date & time Pin
HobodeoGangsta23-Feb-09 6:50
HobodeoGangsta23-Feb-09 6:50 
GeneralCan't see the image Pin
hoshoo8-Feb-07 0:06
professionalhoshoo8-Feb-07 0:06 
QuestionSystem.UnauthorizedAccessException Pin
laxmaiah18-Oct-06 5:56
laxmaiah18-Oct-06 5:56 
AnswerRe: System.UnauthorizedAccessException Pin
Fadi Abdelqader4-Jan-07 12:38
Fadi Abdelqader4-Jan-07 12:38 
General2 webcams Pin
Abeer.Khair8-May-06 23:36
Abeer.Khair8-May-06 23:36 
GeneralTo control web cam Pin
Ajitsurana30-Jan-06 23:59
Ajitsurana30-Jan-06 23:59 
GeneralThe specified module could not be found. Pin
WasimHC131-Dec-05 21:48
WasimHC131-Dec-05 21:48 
GeneralAccess Denied Error Pin
Anonymous24-Oct-05 22:08
Anonymous24-Oct-05 22:08 
GeneralLive video Pin
wergfsdg4-Oct-05 22:20
wergfsdg4-Oct-05 22:20 
QuestionMemory Leak? Pin
l33t_x4-Sep-05 16:24
l33t_x4-Sep-05 16:24 
QuestionPlease, Help us with this error ? Pin
Dream2be29-Apr-05 23:23
Dream2be29-Apr-05 23:23 
AnswerRe: Please, Help us with this error ? Pin
Laurent Kempé17-May-05 4:45
Laurent Kempé17-May-05 4:45 
AnswerRe: Please, Help us with this error ? Pin
Member 195457229-Sep-05 1:39
Member 195457229-Sep-05 1:39 
GeneralRe: Please, Help us with this error ? Pin
zhaojicheng14-Apr-06 14:35
zhaojicheng14-Apr-06 14:35 
GeneralHelp : (ERROR GrabFrame().) Pin
ganeshnet27-Apr-06 19:04
ganeshnet27-Apr-06 19:04 
GeneralRe: Help : (ERROR GrabFrame().) Pin
alex_realex14-Apr-07 0:25
alex_realex14-Apr-07 0:25 
GeneralRe: Help : (ERROR GrabFrame().) Pin
Pavel Suk15-Oct-08 8:59
Pavel Suk15-Oct-08 8:59 
GeneralRe: Help : (ERROR GrabFrame().) Pin
Pavel Suk15-Oct-08 8:59
Pavel Suk15-Oct-08 8:59 
GeneralAccess Denided Pin
besthand7-Mar-05 4:38
besthand7-Mar-05 4:38 
GeneralIT SHOWS ONLY BLUE BOX WITH TIME &amp; DATE Pin
kkyirui8-Dec-04 14:14
kkyirui8-Dec-04 14:14 
GeneralGood Job Pin
sides_dale29-Aug-04 15:57
sides_dale29-Aug-04 15:57 
QuestionHow to show my picture in my Webcam on ASP.NET Pin
vuhoangquocanh29-Jul-04 7:31
vuhoangquocanh29-Jul-04 7:31 

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.