Click here to Skip to main content
Click here to Skip to main content

Dynamic Webcam Image

, 24 Jun 2002
Rate this:
Please Sign up or sign in to vote.
Streaming Dynamic Images from your webcam into your web pages.
<!-- Download Links -->

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

Share

About the Author

Laurent Kempé
Architect
France France

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

Follow on   Twitter

Comments and Discussions

 
QuestionHard code default webcam possible? PinmemberAakshay Subu2-Apr-14 20:08 
Questionun error Pinmembertobias0227-Aug-13 18:39 
QuestionCapture Image from Web Cam Asp.net application PinmemberMember 991393816-May-13 21:30 
QuestionRe: Capture Image from Web Cam Asp.net application Pinmembermanipriya62110-Jul-13 0:27 
QuestionError PinmemberAshish Rathod15-Dec-12 0:22 
QuestionI need codes about how to cotrol camera Pinmemberxianzhi yin2-May-12 16:04 
QuestionRequest for IP camera C++ code PinmemberWinson_19894-Apr-12 22:53 
GeneralMy vote of 1 PinmemberG RAGHUVEER28-Feb-12 23:56 
QuestionOnly Date and Black Screen PinmemberHewbert Gabon23-Jan-12 7:16 
AnswerRe: Only Date and Black Screen PinmemberMember 86139702-Feb-12 6:16 
QuestionRe: Retrieving the COM class factory for component with CLSID {D6BA1539-8473-497C-92C3-8ECF63DAC0F3} failed due to the following error: 80040154 Pinmemberpuneetisonfire8918-Oct-11 0:17 
AnswerRe: Retrieving the COM class factory for component with CLSID {D6BA1539-8473-497C-92C3-8ECF63DAC0F3} failed due to the following error: 80040154 PinmemberRaghava jogu2-Apr-12 22:58 
GeneralRe: Retrieving the COM class factory for component with CLSID {D6BA1539-8473-497C-92C3-8ECF63DAC0F3} failed due to the following error: 80040154 PinmemberNwCaos22-Apr-13 8:16 
Questionyour project is error ridden and not working Pinmemberinternational ninja28-Sep-11 8:59 
Questionimage capturing only from my system web cam if i upload the code in server Pinmemberrajender3813-Sep-11 1:47 
hi this is rajender, i need a help for this problem. everything is working fine with this code but the problem is, when i deploy this in server, it is capturing the image only from my system. Instead i want to capture the image whereever the application is running.
GeneralUnable to add reference file CamServer.dll Pinmember56456kj4-Jun-11 11:36 
GeneralProblem to register PinmemberUnknown Ajanabi20-Jan-11 22:14 
GeneralRe: Problem to register PinmemberMember 794624723-May-11 0:57 
GeneralMy vote of 5 PinmemberJoe Sonderegger14-Jan-11 2:58 
GeneralVirus Found Pinmemberscienty775-Jan-10 22:59 
GeneralRe: Virus Found PinmemberLaurent Kempé5-Jan-10 23:19 
GeneralBlank Image Show Pinmemberpiyush.sabla@e-connectsolutions.com28-Nov-09 1:34 
GeneralRe: Blank Image Show Pinmemberpuneetisonfire8917-Oct-11 21:27 
GeneralRetrieving the COM class factory for component with CLSID {D6BA1539-8473-497C-92C3-8ECF63DAC0F3} failed due to the following error: 80040154 Pinmemberajau.jain23-Nov-09 3:38 
GeneralRe: Retrieving the COM class factory for component with CLSID {D6BA1539-8473-497C-92C3-8ECF63DAC0F3} failed due to the following error: 80040154 Pinmemberpuneetisonfire8917-Oct-11 21:00 
QuestionERROR GrabFrame() Pinmemberselvakumarbt24-Aug-09 2:10 
AnswerRe: ERROR GrabFrame() Pinmemberroshilkdecs30-Sep-09 23:09 
GeneralStoring in database PinmemberMember 33558209-Apr-09 22:17 
General[My vote of 1] hi need help Pinmemberfiras_maher8-Apr-09 23:58 
QuestionWebcam: Registering CamServer.dll PinmemberTushar.Johri14-Nov-08 13:55 
Questionstoring the images Pinmemberjajjssddd23-Sep-08 22:12 
GeneralThanks PinmemberFastfootskater12-Jul-08 7:55 
GeneralPanda Software alert of virus on the project Pinmembernick716-May-08 19:31 
GeneralRe: Panda Software alert of virus on the project PinmemberLaurent Kempé6-May-08 23:27 
Generalregister the dll Pinmemberriccardodaria28-Apr-08 6:43 
Questionhow to this capture image in database Pinmembermastermehta26-Feb-08 22:00 
Generalneed capture the photo in client system Pinmemberarumguavel24-Feb-08 20:36 
GeneralRe: need capture the photo in client system Pinmembervenkeeyy14-Apr-08 22:26 
Generalwant to check web cam connect or not Pinmemberarumguavel30-Jan-08 19:07 
Generalhelp Pinmemberizzettamer2-Oct-07 2:14 
GeneralRe: help Pinmemberdaldal13-Oct-07 8:35 
GeneralERROR GrabFrame(). PinmemberHariom narayan20-Jul-07 2:21 
GeneralRe: ERROR GrabFrame(). Pinmembershamaaaaaaaaaaaaaa26-Apr-08 3:30 
GeneralRe: ERROR GrabFrame(). PinmemberMember 57512753-Dec-08 22:46 
GeneralRe: ERROR GrabFrame(). Pinmemberjonno00716-Nov-09 4:45 
GeneralRe: ERROR GrabFrame(). Pinmember~~BAKKAR~~23-Oct-08 3:31 
GeneralRe: ERROR GrabFrame().WebDev.WebServer.exe[1348] Pinmemberajau.jain18-Feb-09 0:13 
GeneralRe: ERROR GrabFrame(). Pinmemberk_catch5-Aug-10 7:32 
GeneralThanks! PinmemberRonald Colijn18-Jun-07 0:43 
GeneralRe: Thanks! Pinmembersumantmunna24-Jun-07 21:21 

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.

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