Click here to Skip to main content
5,790,650 members and growing! (22,762 online)
Email Password   helpLost your password?
Web Development » Charts, Graphs and Images » Images and multimedia     Intermediate

Dynamic Webcam Image

By Laurent Kempé

Streaming Dynamic Images from your webcam into your web pages.
C#, Windows, .NET 1.0, .NET, Visual Studio, ASP.NET, Dev

Posted: 24 Jun 2002
Updated: 24 Jun 2002
Views: 220,929
Bookmarked: 87 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
19 votes for this Article.
Popularity: 5.36 Rating: 4.19 out of 5
1 vote, 7.7%
1
1 vote, 7.7%
2
0 votes, 0.0%
3
3 votes, 23.1%
4
8 votes, 61.5%
5

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é




Laurent Kempé is the editor, founder, and primary contributor of Tech Head Brothers, a French portal about Microsoft .NET technologies.

Laurent is awarded by Microsoft since Avril 2002: Most Valuable Professional (MVP). You might contact him through his Blog.



Occupation: Web Developer
Location: France France

Other popular Charts, Graphs and Images articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 89 (Total in Forum: 89) (Refresh)FirstPrevNext
QuestionWebcam: Registering CamServer.dllmemberTushar.Johri13:55 14 Nov '08  
Questionstoring the imagesmemberjajjssddd22:12 23 Sep '08  
GeneralThanksmemberFastfootskater7:55 12 Jul '08  
GeneralPanda Software alert of virus on the projectmembernick7119:31 6 May '08  
GeneralRe: Panda Software alert of virus on the projectmemberLaurent Kempé23:27 6 May '08  
Generalregister the dllmemberriccardodaria6:43 28 Apr '08  
Generalhow to this capture image in databasemembermastermehta22:00 26 Feb '08  
Generalneed capture the photo in client systemmemberarumguavel20:36 24 Feb '08  
GeneralRe: need capture the photo in client systemmembervenkeeyy22:26 14 Apr '08  
Generalwant to check web cam connect or notmemberarumguavel19:07 30 Jan '08  
Generalhelpmemberizzettamer2:14 2 Oct '07  
GeneralRe: helpmemberdaldal8:35 13 Oct '07  
GeneralERROR GrabFrame().memberHariom narayan2:21 20 Jul '07  
GeneralRe: ERROR GrabFrame().membershamaaaaaaaaaaaaaa3:30 26 Apr '08  
GeneralRe: ERROR GrabFrame().memberMember 575127522:46 3 Dec '08  
GeneralRe: ERROR GrabFrame().member~~BAKKAR~~3:31 23 Oct '08  
GeneralThanks!memberRonald Colijn0:43 18 Jun '07  
GeneralRe: Thanks!membersumantmunna21:21 24 Jun '07  
Generalerrormemberizzettamer3:23 1 Oct '07  
Generalblack image with date & timemembershiva12341:34 14 May '07  
GeneralRe: black image with date & timememberpask21:13 6 Jun '07  
GeneralCan't see the imagememberhoshoo1:06 8 Feb '07  
QuestionSystem.UnauthorizedAccessExceptionmemberlaxmaiah6:56 18 Oct '06  
AnswerRe: System.UnauthorizedAccessExceptionmemberFadi Abdelqader , www.fadidotnet.org13:38 4 Jan '07  
General2 webcamsmemberAbeer.Khair0:36 9 May '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Jun 2002
Editor: Chris Maunder
Copyright 2002 by Laurent Kempé
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project