Click here to Skip to main content
15,879,535 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 553K   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

 
QuestionHard code default webcam possible? Pin
Aakshay Subu2-Apr-14 19:08
Aakshay Subu2-Apr-14 19:08 
Questionun error Pin
tobias0227-Aug-13 17:39
tobias0227-Aug-13 17:39 
QuestionCapture Image from Web Cam Asp.net application Pin
Member 991393816-May-13 20:30
Member 991393816-May-13 20:30 
QuestionRe: Capture Image from Web Cam Asp.net application Pin
manipriya6219-Jul-13 23:27
manipriya6219-Jul-13 23:27 
QuestionError Pin
Ashish Rathod14-Dec-12 23:22
Ashish Rathod14-Dec-12 23:22 
QuestionI need codes about how to cotrol camera Pin
xianzhi yin2-May-12 15:04
xianzhi yin2-May-12 15:04 
QuestionRequest for IP camera C++ code Pin
Winson_19894-Apr-12 21:53
Winson_19894-Apr-12 21:53 
GeneralMy vote of 1 Pin
G RAGHUVEER28-Feb-12 22:56
G RAGHUVEER28-Feb-12 22:56 
QuestionOnly Date and Black Screen Pin
Hewbert Gabon23-Jan-12 6:16
Hewbert Gabon23-Jan-12 6:16 
AnswerRe: Only Date and Black Screen Pin
Member 86139702-Feb-12 5:16
Member 86139702-Feb-12 5:16 
QuestionRe: Retrieving the COM class factory for component with CLSID {D6BA1539-8473-497C-92C3-8ECF63DAC0F3} failed due to the following error: 80040154 Pin
puneetisonfire8917-Oct-11 23:17
puneetisonfire8917-Oct-11 23:17 
AnswerRe: Retrieving the COM class factory for component with CLSID {D6BA1539-8473-497C-92C3-8ECF63DAC0F3} failed due to the following error: 80040154 Pin
Raghava jogu2-Apr-12 21:58
Raghava jogu2-Apr-12 21:58 
GeneralRe: Retrieving the COM class factory for component with CLSID {D6BA1539-8473-497C-92C3-8ECF63DAC0F3} failed due to the following error: 80040154 Pin
NwCaos22-Apr-13 7:16
NwCaos22-Apr-13 7:16 
Questionyour project is error ridden and not working Pin
international ninja28-Sep-11 7:59
international ninja28-Sep-11 7:59 
ERROR GrabFrame().
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.COMException: ERROR GrabFrame().

Source Error:

Line 21: CAMSERVERLib.Camera cam = new CAMSERVERLib.CameraClass();
Line 22:
Line 23: byte[] picture = (byte[])cam.GrabFrame(nQuality);
Line 24:
Line 25: //Add the hour to the jpeg picture


Source File: c:\Users\markd.JELLYSTONE\Documents\Visual Studio 2008\Websites\LatestWebCamTest\Default.aspx.cs Line: 23

Stack Trace:

[COMException (0x80040001): ERROR GrabFrame().]
CAMSERVERLib.CameraClass.GrabFrame(Int16 nQuality) +0
_Default.Page_Load(Object sender, EventArgs e) in c:\Users\markd.JELLYSTONE\Documents\Visual Studio 2008\Websites\LatestWebCamTest\Default.aspx.cs:23
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

many people have this same error yet you have not responded. the display of 24bit or higher is not the problem. Please give me a real solution with out guessing at random things.

why are we getting a GrabFrame error? you wrote the com if you dont have time to fix your code take your project down cause it doesn't work.
Questionimage capturing only from my system web cam if i upload the code in server Pin
rajender3813-Sep-11 0:47
rajender3813-Sep-11 0:47 
GeneralUnable to add reference file CamServer.dll Pin
56456kj4-Jun-11 10:36
56456kj4-Jun-11 10:36 
GeneralProblem to register Pin
Unknown Ajanabi20-Jan-11 21:14
Unknown Ajanabi20-Jan-11 21:14 
GeneralRe: Problem to register Pin
Member 794624722-May-11 23:57
Member 794624722-May-11 23:57 
GeneralMy vote of 5 Pin
Joe Sonderegger14-Jan-11 1:58
Joe Sonderegger14-Jan-11 1:58 
GeneralVirus Found Pin
scienty775-Jan-10 21:59
scienty775-Jan-10 21:59 
GeneralRe: Virus Found Pin
Laurent Kempé5-Jan-10 22:19
Laurent Kempé5-Jan-10 22:19 
GeneralBlank Image Show Pin
piyush.sabla@e-connectsolutions.com28-Nov-09 0:34
piyush.sabla@e-connectsolutions.com28-Nov-09 0:34 
GeneralRe: Blank Image Show Pin
puneetisonfire8917-Oct-11 20:27
puneetisonfire8917-Oct-11 20:27 
GeneralRetrieving the COM class factory for component with CLSID {D6BA1539-8473-497C-92C3-8ECF63DAC0F3} failed due to the following error: 80040154 Pin
ajau.jain23-Nov-09 2:38
ajau.jain23-Nov-09 2:38 
GeneralRe: Retrieving the COM class factory for component with CLSID {D6BA1539-8473-497C-92C3-8ECF63DAC0F3} failed due to the following error: 80040154 Pin
puneetisonfire8917-Oct-11 20:00
puneetisonfire8917-Oct-11 20:00 

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.