Solved

webcam

Posted on 2008-06-26
8
1,301 Views
Last Modified: 2013-12-16
Live steraming by webcam
0
Comment
Question by:haranath1245
  • 4
  • 4
8 Comments
 
LVL 10

Expert Comment

by:athapa
ID: 21880774
What about live streaming from webcam?
0
 

Author Comment

by:haranath1245
ID: 21880870
Hi,

I want to know how use web cam  in c# web application
0
 
LVL 10

Expert Comment

by:athapa
ID: 21881877

You can use WAI apis or directx or SDKs provided by your webcam providers (if they have one).


Take a look at these articles and code samples.
http://www.codeproject.com/KB/audio-video/webcamservice.aspx

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=1339&lngWId=10

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:haranath1245
ID: 21892948
Hi,

In the links u have given consists of windows application. But i want to develop in web application. Can u please Help.

Thanks
0
 
LVL 10

Expert Comment

by:athapa
ID: 21894357
If a refresh rate of few seconds or so is ok (i.e. you don't need 30 frames per seconds) then use those windows app examples (or convert them to windows services) and have that application capture each frame and save in a folder inside web app. One of the page in your web app could then display the image and refresh itself every x number of seconds using meta-refresh tag.or javascript.
Some web cams come with applications to ftp what it captures. You could have the camera ftp to your web apps folder.

If you want higher framerates then you need to look at Windows Media Encoder.
http://www.microsoft.com/windows/windowsmedia/forpros/encoder/default.mspx.

This discussion explains how to use Windows Media Encoder.
http://forums.logitech.com/logitech/board/message?board.id=general_webcams&thread.id=8427

The user can now view the webcam through media player. If you want that to be embedded in your web page then you can embedded Media Player in your web application.

0
 

Author Comment

by:haranath1245
ID: 21896647
Thank a lot.

Can i have some sample code.

Thanks
0
 
LVL 10

Expert Comment

by:athapa
ID: 21912933
Take a look at this article first
http://www.codeproject.com/KB/cpp/dimewebcam.aspx


The code below, copied from http://channel9.msdn.com/forums/TechOff/93476-Programatically-Using-A-Webcam-In-C/., and the few other links below will let you capture frames. You can then save those frames in a webserver and display through your web pages.

Here are few others
http://blogs.msdn.com/coding4fun/archive/2006/10/31/912546.aspx

http://blogs.msdn.com/markhsch/archive/2007/11/19/c-webcam-user-control-source.aspx


//Copied from http://channel9.msdn.com/forums/TechOff/93476-Programatically-Using-A-Webcam-In-C/
 
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
 
namespace WebCam_Capture
{
 /// <summary>
 /// Summary description for UserControl1.
 /// </summary>
 [System.Drawing.ToolboxBitmap(typeof(WebCamCapture), "CAMERA.ICO")] // toolbox bitmap
 [Designer("Sytem.Windows.Forms.Design.ParentControlDesigner,System.Design", typeof(System.ComponentModel.Design.IDesigner))] // make composite
 public class WebCamCapture : System.Windows.Forms.UserControl
 {
  private System.ComponentModel.IContainer components;
  private System.Windows.Forms.Timer timer1;
 
  // property variables
  private int m_TimeToCapture_milliseconds = 100;
  private int m_Width = 320;
  private int m_Height = 240;
  private int mCapHwnd;
  private ulong m_FrameNumber = 0;
 
  // global variables to make the video capture go faster
  private WebCam_Capture.WebcamEventArgs x = new WebCam_Capture.WebcamEventArgs();
  private IDataObject tempObj;
  private System.Drawing.Image tempImg;
  private bool bStopped = true;
 
  // event delegate
  public delegate void WebCamEventHandler (object source, WebCam_Capture.WebcamEventArgs e);
  // fired when a new image is captured
  public event WebCamEventHandler ImageCaptured;
 
  #region API Declarations
 
  [DllImport("user32", EntryPoint="SendMessage")]
  public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
 
  [DllImport("avicap32.dll", EntryPoint="capCreateCaptureWindowA")]
  public static extern int capCreateCaptureWindowA(string lpszWindowName, int dwStyle, int X, int Y, int nWidth, int nHeight, int hwndParent, int nID);
 
  [DllImport("user32", EntryPoint="OpenClipboard")]
  public static extern int OpenClipboard(int hWnd);
 
  [DllImport("user32", EntryPoint="EmptyClipboard")]
  public static extern int EmptyClipboard();
 
  [DllImport("user32", EntryPoint="CloseClipboard")]
  public static extern int CloseClipboard();
 
  #endregion
 
  #region API Constants
 
  public const int WM_USER = 1024;
 
  public const int WM_CAP_CONNECT = 1034;
  public const int WM_CAP_DISCONNECT = 1035;
  public const int WM_CAP_GET_FRAME = 1084;
  public const int WM_CAP_COPY = 1054;
 
  public const int WM_CAP_START = WM_USER;
 
  public const int WM_CAP_DLG_VIDEOFORMAT = WM_CAP_START + 41;
  public const int WM_CAP_DLG_VIDEOSOURCE = WM_CAP_START + 42;
  public const int WM_CAP_DLG_VIDEODISPLAY = WM_CAP_START + 43;
  public const int WM_CAP_GET_VIDEOFORMAT = WM_CAP_START + 44;
  public const int WM_CAP_SET_VIDEOFORMAT = WM_CAP_START + 45;
  public const int WM_CAP_DLG_VIDEOCOMPRESSION = WM_CAP_START + 46;
  public const int WM_CAP_SET_PREVIEW = WM_CAP_START + 50;
 
  #endregion
 
  #region NOTES
 
  /*
   * If you want to allow the user to change the display size and
   * color format of the video capture, call:
   * SendMessage (mCapHwnd, WM_CAP_DLG_VIDEOFORMAT, 0, 0);
   * You will need to requery the capture device to get the new settings
  */
 
  #endregion
 
 
  public WebCamCapture()
  {
   // This call is required by the Windows.Forms Form Designer.
   InitializeComponent();
  }
 
  /// <summary>
  /// Override the class's finalize method, so we can stop
  /// the video capture on exit
  /// </summary>
  ~WebCamCapture()
  {
   this.Stop();
  }
 
  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
 
    if( components != null )
     components.Dispose();
   }
   base.Dispose( disposing );
  }
 
  #region Component Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   this.components = new System.ComponentModel.Container();
   this.timer1 = new System.Windows.Forms.Timer(this.components);
   //
   // timer1
   //
   this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
   //
   // WebCamCapture
   //
   this.Name = "WebCamCapture";
   this.Size = new System.Drawing.Size(342, 252);
   this.Load += new System.EventHandler(this.WebCamCapture_Load);
 
  }
  #endregion
 
  #region Control Properties
 
  /// <summary>
  /// The time intervale between frame captures
  /// </summary>
  public int TimeToCapture_milliseconds
  {
   get
   { return m_TimeToCapture_milliseconds; }
 
   set
   { m_TimeToCapture_milliseconds = value; }
  }
 
  /// <summary>
  /// The height of the video capture image
  /// </summary>
  public int CaptureHeight
  {
   get
   { return m_Height; }
   
   set
   { m_Height = value; }
  }
 
  /// <summary>
  /// The width of the video capture image
  /// </summary>
  public int CaptureWidth
  {
   get
   { return m_Width; }
 
   set
   { m_Width = value; }
  }
 
  /// <summary>
  /// The sequence number to start at for the frame number. OPTIONAL
  /// </summary>
  public ulong FrameNumber
  {
   get
   { return m_FrameNumber; }
 
   set
   { m_FrameNumber = value; }
  }
 
  #endregion
 
  #region Start and Stop Capture Functions
 
  /// <summary>
  /// Starts the video capture
  /// </summary>
  /// <param name="FrameNumber">the frame number to start at.
  /// Set to 0 to let the control allocate the frame number</param>
  public void Start(ulong FrameNum)
  {
   try
   {
    // for safety, call stop, just in case we are already running
    this.Stop();
 
    // setup a capture window
    mCapHwnd = capCreateCaptureWindowA("WebCap", 0, 0, 0, m_Width, m_Height, this.Handle.ToInt32(), 0);
 
    // connect to the capture device
    Application.DoEvents();
    SendMessage(mCapHwnd, WM_CAP_CONNECT, 0, 0);
    SendMessage(mCapHwnd, WM_CAP_SET_PREVIEW, 0, 0);
 
    // set the frame number
    m_FrameNumber = FrameNum;
 
    // set the timer information
    this.timer1.Interval = m_TimeToCapture_milliseconds;
    bStopped = false;
    this.timer1.Start();
   }
 
   catch (Exception excep)
   {
    MessageBox.Show("An error ocurred while starting the video capture. Check that your webcamera is connected properly and turned on.\r\n\n" + excep.Message);
    this.Stop();
   }
  }
 
  /// <summary>
  /// Stops the video capture
  /// </summary>
  public void Stop()
  {
   try
   {
    // stop the timer
    bStopped = true;
    this.timer1.Stop();
 
    // disconnect from the video source
    Application.DoEvents();
    SendMessage(mCapHwnd, WM_CAP_DISCONNECT, 0, 0);
   }
 
   catch (Exception excep)
   { // don't raise an error here.
   }
 
  }
 
  #endregion
 
  #region Video Capture Code
 
  /// <summary>
  /// Capture the next frame from the video feed
  /// </summary>
  private void timer1_Tick(object sender, System.EventArgs e)
  {
   try
   {
    // pause the timer
    this.timer1.Stop();
 
    // get the next frame;
    SendMessage(mCapHwnd, WM_CAP_GET_FRAME, 0, 0);
 
    // copy the frame to the clipboard
    SendMessage(mCapHwnd, WM_CAP_COPY, 0, 0);
 
    // paste the frame into the event args image
    if (ImageCaptured != null)
    {
     // get from the clipboard
     tempObj = Clipboard.GetDataObject();
     tempImg = (System.Drawing.Bitmap) tempObj.GetData(System.Windows.Forms.DataFormats.Bitmap);
 
     /*
     * For some reason, the API is not resizing the video
     * feed to the width and height provided when the video
     * feed was started, so we must resize the image here
     */
     x.WebCamImage = tempImg.GetThumbnailImage(m_Width, m_Height, null, System.IntPtr.Zero);
 
     // raise the event
     this.ImageCaptured(this, x);
    }  
 
    // restart the timer
    Application.DoEvents();
    if (! bStopped)
     this.timer1.Start();
   }
 
   catch (Exception excep)
   {
    MessageBox.Show("An error ocurred while capturing the video image. The video capture will now be terminated.\r\n\n" + excep.Message);
    this.Stop(); // stop the process
   }
  }
 
  #endregion
 
  private void WebCamCapture_Load(object sender, System.EventArgs e)
  {
  
  }
 }
}

Open in new window

0
 

Accepted Solution

by:
haranath1245 earned 0 total points
ID: 21922682
Thank a lot.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# Offline Apllication 5 70
What namespace do I need to import? 2 25
VB.NET 2008 (3.5 Framework) Remove all items from List 3 21
VB: Convert 2 dates to specific format 24 48
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now