C# .NET - capture image in web - Asked By Ranjit on 07-Jul-11 01:56 AM

tell me the whole code and procedure that how i can capture the photo from laptop camera
Riley K replied to Ranjit on 07-Jul-11 02:02 AM

The Emgu cv library is overkill for this, but here is an example in 7 lines:

ImageViewer viewer = new ImageViewer(); //create an image viewer
Capture capture = new Capture(); //create a camera captue
Application.Idle += new EventHandler(delegate(object sender, EventArgs e)
{  //run this until application closed (close button click on image viewer)
   viewer.Image = capture.QueryFrame(); //draw the image obtained from camera
});
viewer.ShowDialog(); //show the image viewer

(source: http://emgu.com/wiki/index.php/Camera_Capture_in_7_lines_of_code)

Ravi S replied to Ranjit on 07-Jul-11 02:03 AM
HI

refer this

here are with examples


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

http://www.codeproject.com/KB/graphics/IECapture.aspx

http://www.physicsforums.com/showthread.php?t=364444
Jitendra Faye replied to Ranjit on 07-Jul-11 02:04 AM

Follow this code-

//WebCam.cs

using System;
using System.Collections.Generic;
using System.Text;
using WIALib;
using WIAVIDEOLib;
using System.Runtime.InteropServices;
namespace WebCamLibrary
{
    /// <SUMMARY>
    /// This Class will initialize the camera using Windows Image Acquisition (WIA).
    /// Also, provides methods to view all connected cameras, capture single frame.
    /// </SUMMARY>
    public class WebCam
    {
      private static WebCam WebCamObj;
      /// <SUMMARY>
      ///  variables needed
      /// </SUMMARY>
      private WIAVIDEOLib.WiaVideo WiaVideoObj;
      private WIALib.Wia WiaObj;
      private WIALib.DeviceInfo DefaultDevice;
      private WIALib.DeviceInfo[] Devices;
      private bool DeviceIsOpen;
      /// <SUMMARY>
      /// Initialize The WebCam Class
      /// </SUMMARY>
      private WebCam()
      {
        DeviceIsOpen = false;
        Initialize();
        SetDefaultDevice();
        OpenDefaultDevice();
      }
      /// <SUMMARY>
      /// Please use this method to create a WebCam Object.
      /// </SUMMARY>
      /// <RETURNS>WebCam Object</RETURNS>
      public static WebCam NewWebCam()
      {
        if (WebCam.WebCamObj == null)
          WebCam.WebCamObj = new WebCam();

        return WebCam.WebCamObj;
      }
      private void Initialize()
      {
        if (this.WiaObj == null)
        {
          this.WiaObj = new WiaClass();
        }
        if (this.WiaVideoObj == null)
        {
          this.WiaVideoObj = new WiaVideoClass();
          this.WiaVideoObj.ImagesDirectory = System.IO.Path.GetTempPath();
        }
      }
      /// <SUMMARY>
      /// Use it to close any open resources.
      /// </SUMMARY>
      public void CloseResources()
      {
        try
        {
          if (WiaObj != null)
            Marshal.FinalReleaseComObject(WiaObj);
          if (WiaVideoObj != null)
            Marshal.FinalReleaseComObject(WiaVideoObj);
          if (DefaultDevice != null)
            Marshal.FinalReleaseComObject(DefaultDevice);
          if (Devices != null)
            Marshal.FinalReleaseComObject(Devices);

          DeviceIsOpen = false;
          GC.Collect();
        }
        catch (Exception e)
        {
          System.Diagnostics.Debug.WriteLine(e.Message);
        }
      }
      private void SetDefaultDevice()
      {
        CloseDefaultDevice();

        System.Collections.Generic.List<WIALIB.DEVICEINFOCLASS> devs =
                      new List<DEVICEINFOCLASS>();
        WIALib.Collection devsCol =
                this.WiaObj.Devices as WIALib.CollectionClass;
        foreach (object obj in devsCol)
        {
          WIALib.DeviceInfoClass dev = (WIALib.DeviceInfoClass)
          System.Runtime.InteropServices.Marshal.CreateWrapperOfType(
          obj, typeof(WIALib.DeviceInfoClass));
          if (dev.Type.Contains("Video") == true)
          {
            devs.Add(dev);
          }
          else
          {
            Marshal.FinalReleaseComObject(obj);
            Marshal.FinalReleaseComObject(dev);
          }
        }
        Devices = devs.ToArray();
        if (Devices.Length > 0)
        {
          DefaultDevice = Devices[0];
        }
        else
        {
          throw new Exception("No Cameras Detected");
        }

        Marshal.FinalReleaseComObject(devsCol);
        GC.ReRegisterForFinalize(devs);
      }
      /// <SUMMARY>
      /// Get Connected Cameras
      /// </SUMMARY>
      /// <RETURNS>string[] contains names of connected cameras</RETURNS>
      public string[] GetConnectedCameras()
      {
        string[] devs = new string[this.Devices.Length];
        for (int i = 0; i < devs.Length; i++)
        {
          devs[i] = this.Devices[i].Name;
        }
        return devs;
      }
      private void CloseDefaultDevice()
      {
        if (this.DeviceIsOpen == false)
          return;
        try
        {
          if (this.WiaVideoObj != null)
          {
            this.WiaVideoObj.DestroyVideo();
            Marshal.FinalReleaseComObject(this.DefaultDevice);
            DeviceIsOpen = false;
          }
        }
        catch (Exception e)
        {
          System.Diagnostics.Debug.WriteLine(e.Message);
        }
        finally
        {
          DeviceIsOpen = false;
        }
      }
      /// <SUMMARY>
      /// Use this method to set the camera to capture
      /// from if you have more than one camera.
      /// </SUMMARY>
      /// Name of the camera device.
      /// Get name from GetConnectedCameras()
      /// <RETURNS></RETURNS>
      public bool SetDefaultDevice(string name)
      {
        CloseDefaultDevice();

        for (int i = 0; i < Devices.Length; i++)
        {
          if (Devices[i].Name.Equals(name))
          {
            DefaultDevice = Devices[i];
            return true;
          }
        }
        return false;
      }
      private bool OpenDefaultDevice()
      {
        CloseDefaultDevice();

        try
        {
          this.WiaVideoObj.PreviewVisible = 0;
          this.WiaVideoObj.CreateVideoByWiaDevID(this.DefaultDevice.Id,
                             IntPtr.Zero, 0, 0);
          System.Threading.Thread.CurrentThread.Join(3000);
          this.DeviceIsOpen = true;
          this.WiaVideoObj.Play();
          System.Threading.Thread.CurrentThread.Join(3000);
        }
        catch (Exception e)
        {
          System.Diagnostics.Debug.WriteLine(e.Message);
          CloseDefaultDevice();
          return false;
        }

        return true;
      }
      /// <SUMMARY>
      /// Use it to grab a frame. Use System.IO.MemoryStream to load image
      /// </SUMMARY>
      /// <RETURNS>byte array of the captured image</RETURNS>
      public byte[] GrabFrame()
      {
        string imagefile;
        this.WiaVideoObj.TakePicture(out imagefile);
        return ReadImageFile(imagefile);
      }
      private byte[] ReadImageFile(string img)
      {
        System.IO.FileInfo fileInfo = new System.IO.FileInfo(img);
        byte[] buf = new byte[fileInfo.Length];
        System.IO.FileStream fs = new System.IO.FileStream(img,
            System.IO.FileMode.Open,System.IO.FileAccess.Read);
        fs.Read(buf, 0, buf.Length);
        fs.Close();
        fileInfo.Delete();
        GC.ReRegisterForFinalize(fileInfo);
        GC.ReRegisterForFinalize(fs);
        return buf;
      }
    }
}


//WebCamService.cs

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://www.codeproject.com")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebCamService : System.Web.Services.WebService
{
    private WebCamLibrary.WebCam cam;

    public WebCamService()
    {
      //Uncomment the following line if using designed components
      //InitializeComponent();
      cam = WebCamLibrary.WebCam.NewWebCam();   
    }
    [WebMethod]
    public byte[] GrabFrame() {
      return cam.GrabFrame();
    }
    [WebMethod]
    public string[] GetConnectedCameras()
    {
      return cam.GetConnectedCameras();
    }
}

Hope this will help you.

TSN ... replied to Ranjit on 07-Jul-11 02:04 AM
hi..

here it is,,

Walkthrough

I. First let's explain what we should do exactly.
  1. At the contrast of the previous demonstrated article where the user is invited to select the device via a dialog box, the device will be selected only through code this once.

  2. After selecting and connecting to the device, I'll try to invoke some external functions using the PInvoke platform in order to integrate the web cam functionalities in my application. To do that, I will wrap all the unmanaged code in a class so that the user deals indirectly with the unmanaged code via this one.
II. Second let's explain and indicates how to integrate the avicap32.dll in the application and witches functions are invoked via the PInvoke platform to integrate the web cam functionalities:

The avicap32.dll is normally located in the WINDOWS\System32, but if it is not found you can download it from this link:

http://www.dll-files.com/dllindex/dll-files.shtml?avicap32

After downloading it and verifying whether there are potentials risks or not when using the given assembly, a measure should be taken in this context by the way.

The measure is to check the compatibility between the downloaded component and your system in order to prevent troubles caused by bugs, therefore it is recommended to download and use this tool:

http://www.liutilities.com/products/campaigns/affiliatehttp://www.liutilities.com/products/campaigns/affiliate/cb/offer/dllfiles/rb/http://www.liutilities.com/products/campaigns/affiliate/cb/offer/dllfiles/rb/

Copy and paste the given assembly in C:\WINDOWS\System32 and then you have to register it using the regsvr32.exe tool.



Figure 1.

But all those steps might not be necessary because in almost cases avicap32.dll is found in the System32. Now, let's see witches functions should be used to integrate the web cam functionalities.


Figure 2.

This is how the class Wrapper should be :

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WebCamera
{
   public class WebCam: IDisposable
{
    /* Those contants are used to overload the unmanaged code functions
   * each constant represent a state*/
 
    private const short WM_CAP = 0x400;
    private const int WM_CAP_DRIVER_CONNECT = 0x40a;
    private const int WM_CAP_DRIVER_DISCONNECT = 0x40b;
    private const int WM_CAP_EDIT_COPY = 0x41e;
    private const int WM_CAP_SET_PREVIEW = 0x432;
    private const int WM_CAP_SET_OVERLAY = 0x433;
    private const int WM_CAP_SET_PREVIEWRATE = 0x434;
    private const int WM_CAP_SET_SCALE = 0x435;
    private const int WS_CHILD = 0x40000000;
    private const int WS_VISIBLE = 0x10000000;
    private const short SWP_NOMOVE = 0x2;
    private short SWP_NOZORDER = 0x4;
    private short HWND_BOTTOM = 1;
 
    //This function enables enumerate the web cam devices
    [DllImport("avicap32.dll")]
    protected static extern bool capGetDriverDescriptionA(short wDriverIndex,
      [MarshalAs(UnmanagedType.VBByRefStr)]ref String lpszName,
     int cbName, [MarshalAs(UnmanagedType.VBByRefStr)] ref String lpszVer, int cbVer);

    //This function enables create a  window child with so that you can display it in a picturebox for example
    [DllImport("avicap32.dll")]
    protected static extern int capCreateCaptureWindowA([MarshalAs(UnmanagedType.VBByRefStr)] ref string
lpszWindowName,
      int dwStyle, int x, int y, int nWidth, int nHeight, int hWndParent, int nID);

    //This function enables set changes to the size, position, and Z order of a child window
    [DllImport("user32")]
    protected static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);

    //This function enables send the specified message to a window or windows
    [DllImport("user32", EntryPoint = "SendMessageA")]
    protected static extern int SendMessage(int hwnd, int wMsg, int wParam, [MarshalAs(UnmanagedType.AsAny)] object
lParam);

    //This function enable destroy the window child
    [DllImport("user32")]
    protected static extern bool DestroyWindow(int hwnd);
 
    // Normal device ID
    int DeviceID = 0;
    // Handle value to preview window
    int hHwnd = 0;
    //The devices list
    ArrayList ListOfDevices = new ArrayList();
 
    //The picture to be displayed
    public PictureBox Container { get; set; }
 
    // Connect to the device.
    /// <summary>
    /// This function is used to load the list of the devices
    /// </summary>
    public void Load()
    {
      string Name = String.Empty.PadRight(100);
      string Version = String.Empty.PadRight(100);
      bool EndOfDeviceList = false;
      short index = 0;
 
      // Load name of all avialable devices into the lstDevices .
      do
      {
        // Get Driver name and version
        EndOfDeviceList = capGetDriverDescriptionA(index, ref Name, 100, ref Version, 100);
        // If there was a device add device name to the list
        if (EndOfDeviceList) ListOfDevices.Add(Name.Trim());
        index += 1;
      }
      while (!(EndOfDeviceList == false));
    }

    /// <summary>
    /// Function used to display the output from a video capture device, you need to create
    /// a capture window.
    /// </summary>
    public void OpenConnection()
    {
      string DeviceIndex = Convert.ToString(DeviceID);
      IntPtr oHandle = Container.Handle;
     
      {
        MessageBox.Show("You should set the container property");
      }
 
  // Open Preview window in picturebox .
 // Create a child window with capCreateCaptureWindowA so you can display it in a picturebox.
 
    hHwnd = capCreateCaptureWindowA(ref DeviceIndex, WS_VISIBLE | WS_CHILD, 0, 0, 640, 480, oHandle.ToInt32(), 0);

     // Connect to device
     if (SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, DeviceID, 0) != 0)
      {
       // Set the preview scale
       SendMessage(hHwnd, WM_CAP_SET_SCALE, -1, 0);
        // Set the preview rate in terms of milliseconds
        SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0);

       // Start previewing the image from the camera
       SendMessage(hHwnd, WM_CAP_SET_PREVIEW, -1, 0);

   // Resize window to fit in picturebox
   SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, Container.Height, Container.Width, SWP_NOMOVE | SWP_NOZORDER

      }
       else
      {
        // Error connecting to device close window
        DestroyWindow(hHwnd);
      }
    }

   //Close windows
   void CloseConnection()

    {
     SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, DeviceID, 0);
     // close window
      DestroyWindow(hHwnd);
    }

    //Save image

    public void SaveImage()
    {
      IDataObject data;
      Image oImage;
      SaveFileDialog sfdImage = new SaveFileDialog();
      sfdImage.Filter = "(*.bmp)|*.bmp";
      // Copy image to clipboard
      SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0);
 
      // Get image from clipboard and convert it to a bitmap
      data = Clipboard.GetDataObject();
      if (data.GetDataPresent(typeof(System.Drawing.Bitmap)))
      {
     oImage = (Image)data.GetData(typeof(System.Drawing.Bitmap));
     Container.Image = oImage;
     CloseConnection();
     if (sfdImage.ShowDialog() == DialogResult.OK)

     {
        oImage.Save(sfdImage.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
     }
     }
    }

    /// <summary>
    /// This function is used to dispose the connection to the device
    /// </summary>
    #region IDisposable Members

   public void Dispose()

    {     
     CloseConnection();
    }
    #endregion
}
}

If I try to represent it using diagram, it will be represented as follow:



Figure 3.


This class provides three functionalities through three functions
  1. Open Connection : This method opens the connection to the devise
  2. Save Image : This method takes a snapshot view and save it in the hard disk
  3. Dispose : This method releases the connection to the device
And a property :
  1. The container is set to the picture box witch dedicated to display the image streaming.
To consume the services of this class, create a new windows application project and then add a picture box and three buttons to the form so that it appears as below :



Figure 4.

Rename the three buttons respectively as bellow :

  • btnStart

  • btnSave

  • btnStop
Add implement the following code

public partial class Form1 : Form
    {
      public Form1()
      {
        InitializeComponent();
      }
      private void Form1_Load(object sender, EventArgs e)
      {
        oWebCam = new WebCam();
        oWebCam.Container = pictureBox1;
      }
      WebCam oWebCam;
      private void btnStart_Click(object sender, EventArgs e)
      {
        oWebCam.OpenConnection();
      }
     
      private void btnSave_Click(object sender, EventArgs e)
      {
        oWebCam.SaveImage();
      }
 
      private void btnStop_Click(object sender, EventArgs e)
      {
        oWebCam.Dispose();
      }
     
    }

Now, build the application and run it
 



hope this helsp you...