Click here to Skip to main content
6,636,867 members and growing! (20,707 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, ASP.NET, Visual Studio, Dev
Posted:24 Jun 2002
Views:255,818
Bookmarked:102 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
21 votes for this article.
Popularity: 5.45 Rating: 4.12 out of 5
2 votes, 13.3%
1
1 vote, 6.7%
2

3
3 votes, 20.0%
4
9 votes, 60.0%
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é


Member


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
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 97 (Total in Forum: 97) (Refresh)FirstPrevNext
GeneralRetrieving the COM class factory for component with CLSID {D6BA1539-8473-497C-92C3-8ECF63DAC0F3} failed due to the following error: 80040154 Pinmemberajau.jain3:38 23 Nov '09  
QuestionERROR GrabFrame() Pinmemberselvakumarbt2:10 24 Aug '09  
AnswerRe: ERROR GrabFrame() Pinmemberroshilkdecs23:09 30 Sep '09  
GeneralStoring in database PinmemberMember 335582022:17 9 Apr '09  
General[My vote of 1] hi need help Pinmemberfiras_maher23:58 8 Apr '09  
QuestionWebcam: Registering CamServer.dll PinmemberTushar.Johri13:55 14 Nov '08  
Questionstoring the images Pinmemberjajjssddd22:12 23 Sep '08  
GeneralThanks PinmemberFastfootskater7:55 12 Jul '08  
GeneralPanda Software alert of virus on the project Pinmembernick7119:31 6 May '08  
GeneralRe: Panda Software alert of virus on the project PinmemberLaurent Kempé23:27 6 May '08  
Generalregister the dll Pinmemberriccardodaria6:43 28 Apr '08  
Generalhow to this capture image in database Pinmembermastermehta22:00 26 Feb '08  
Generalneed capture the photo in client system Pinmemberarumguavel20:36 24 Feb '08  
GeneralRe: need capture the photo in client system Pinmembervenkeeyy22:26 14 Apr '08  
Generalwant to check web cam connect or not Pinmemberarumguavel19:07 30 Jan '08  
Generalhelp Pinmemberizzettamer2:14 2 Oct '07  
GeneralRe: help Pinmemberdaldal8:35 13 Oct '07  
GeneralERROR GrabFrame(). PinmemberHariom narayan2:21 20 Jul '07  
GeneralRe: ERROR GrabFrame(). Pinmembershamaaaaaaaaaaaaaa3:30 26 Apr '08  
GeneralRe: ERROR GrabFrame(). PinmemberMember 575127522:46 3 Dec '08  
GeneralRe: ERROR GrabFrame(). Pinmemberjonno0074:45 16 Nov '09  
GeneralRe: ERROR GrabFrame(). Pinmember~~BAKKAR~~3:31 23 Oct '08  
GeneralRe: ERROR GrabFrame().WebDev.WebServer.exe[1348] Pinmemberajau.jain0:13 18 Feb '09  
GeneralThanks! PinmemberRonald Colijn0:43 18 Jun '07  
GeneralRe: Thanks! Pinmembersumantmunna21:21 24 Jun '07  

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
Web10 | Advertise on the Code Project