Mangiare Senza Glutine disponibile su App Store

Per altre informazioni scrivi a fabriziocaldarelli@negusweb.it

(English) Capture from webcam in CSharp

Da Programmazione Software.

I want share a class to handle webcam using avicap.ddl (found in windows) in C#, in WPF environment.

This class has made with codes from internet, so i am not the only developer of this code. :-)

At the end of code you will find zip to download with test project, very simple to integrate in your application.

Main class: CaptureDevice.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Drawing;
  9. using System.Windows.Media.Imaging;
  10.  
  11.  
  12. namespace CamCapture
  13. {
  14.     /// <see cref="http://www.pinvoke.net/default.aspx/Constants.WM"/>
  15.     public class Constants
  16.     {
  17.         public const uint WM_CAP = 0x400;
  18.         public const uint WM_CAP_DRIVER_CONNECT = 0x40a;
  19.         public const uint WM_CAP_DRIVER_DISCONNECT = 0x40b;
  20.         public const uint WM_CAP_EDIT_COPY = 0x41e;
  21.         public const uint WM_CAP_SET_PREVIEW = 0x432;
  22.         public const uint WM_CAP_SET_OVERLAY = 0x433;
  23.         public const uint WM_CAP_SET_PREVIEWRATE = 0x434;
  24.         public const uint WM_CAP_SET_SCALE = 0x435;
  25.         public const uint WS_CHILD = 0x40000000;
  26.         public const uint WS_VISIBLE = 0x10000000;
  27.     }
  28.  
  29.     /// <see cref="http://windowssdk.msdn.microsoft.com/en-us/library/ms713477(VS.80).aspx"/>
  30.     public class Avicap32
  31.     {
  32.         /// <see cref="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_capgetdriverdescription.asp"/>
  33.         [DllImport("avicap32.dll")]
  34.         public extern static IntPtr capGetDriverDescription(
  35.             ushort index,
  36.             StringBuilder name,
  37.             int nameCapacity,
  38.             StringBuilder description,
  39.             int descriptionCapacity
  40.         );
  41.  
  42.         /// <see cref="http://msdn.microsoft.com/library/en-us/multimed/htm/_win32_capcreatecapturewindow.asp?frame=true"/>
  43.         [DllImport("avicap32.dll")]
  44.         public extern static IntPtr capCreateCaptureWindow(
  45.             string title,
  46.             uint style,
  47.             int x,
  48.             int y,
  49.             int width,
  50.             int height,
  51.             IntPtr window,
  52.             int id
  53.         );
  54.     }
  55.  
  56.     /// <see cref="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/messagesandmessagequeues.asp"/>
  57.     public class User32
  58.     {
  59.         /// <see cref="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/messagesandmessagequeues/messagesandmessagequeuesreference/messagesandmessagequeuesfunctions/sendmessage.asp"/>
  60.         [DllImport("user32.dll")]
  61.         public static extern IntPtr SendMessage(
  62.             IntPtr hWnd,
  63.             uint Msg,
  64.             IntPtr wParam,
  65.             IntPtr lParam
  66.         );
  67.  
  68.         /// <see cref="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/setwindowpos.asp"/>
  69.         [DllImport("user32.dll")]
  70.         public static extern IntPtr SetWindowPos(
  71.             IntPtr hWnd,
  72.             IntPtr hWndInsertAfter,
  73.             int X,
  74.             int Y,
  75.             int cx,
  76.             int cy,
  77.             uint uFlags
  78.         );
  79.  
  80.         /// <see cref="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/destroywindow.asp"/>
  81.         [DllImport("user32")]
  82.         public static extern IntPtr DestroyWindow(
  83.             IntPtr hWnd
  84.         );
  85.     }
  86. }
  87.  
  88. namespace CamCapture
  89. {
  90.         /// <summary>
  91.         /// This class represents a device that is capable of capturing audio and video
  92.         /// </summary>
  93.         public class CaptureDevice
  94.         {
  95.             private static int MAX_DEVICES = 10;
  96.  
  97.             private ushort deviceNumber;
  98.             private string name;
  99.             private string description;
  100.             private IntPtr deviceHandle;
  101.  
  102.             /// <summary>
  103.             /// Constructor
  104.             /// </summary>
  105.             /// <param name="deviceNumber">the number</param>
  106.             /// <param name="name">the name</param>
  107.             /// <param name="description">the description</param>
  108.             private CaptureDevice(ushort deviceNumber, string name, string description)
  109.             {
  110.                 this.deviceNumber = deviceNumber;
  111.                 this.name = name;
  112.                 this.description = description;
  113.             }
  114.  
  115.             /// <summary>
  116.             /// Setter and Getter for the Device Number
  117.             /// </summary>
  118.             public ushort DeviceNumber
  119.             {
  120.                 get { return deviceNumber; }
  121.                 set { deviceNumber = value; }
  122.             }
  123.  
  124.             /// <summary>
  125.             /// Setter and Getter for the Device name
  126.             /// </summary>
  127.             public string Name
  128.             {
  129.                 get { return name; }
  130.                 set { name = value; }
  131.             }
  132.  
  133.             /// <summary>
  134.             /// Setter and Getter for the Device description
  135.             /// </summary>
  136.             public string Description
  137.             {
  138.                 get { return description; }
  139.                 set { description = value; }
  140.             }
  141.  
  142.             /// <summary>
  143.             /// Attaches the preview stream to the given control
  144.             /// </summary>
  145.             /// <param name="control">the control</param>
  146.             public void Attach(System.Windows.Forms.PictureBox control)
  147.             {
  148.                 deviceHandle = Avicap32.capCreateCaptureWindow("", Constants.WS_VISIBLE | Constants.WS_CHILD, 0, 0, control.Width, control.Height, control.Handle, 0);
  149.  
  150.                 if (User32.SendMessage(deviceHandle, Constants.WM_CAP_DRIVER_CONNECT, (IntPtr)deviceNumber, (IntPtr)0).ToInt32() > 0)
  151.                 {
  152.                     User32.SendMessage(deviceHandle, Constants.WM_CAP_SET_SCALE, (IntPtr)(-1), (IntPtr)0);
  153.                     User32.SendMessage(deviceHandle, Constants.WM_CAP_SET_PREVIEWRATE, (IntPtr)0x42, (IntPtr)0);
  154.                     User32.SendMessage(deviceHandle, Constants.WM_CAP_SET_PREVIEW, (IntPtr)(-1), (IntPtr)0);
  155.                     User32.SetWindowPos(deviceHandle, new IntPtr(0), 0, 0, control.Width, control.Height, 6);
  156.                 }
  157.             }
  158.  
  159.             /// <summary>
  160.             /// Detaches from the control
  161.             /// </summary>
  162.             public void Detach()
  163.             {
  164.                 if (deviceHandle.ToInt32() != 0)
  165.                 {
  166.                     User32.SendMessage(deviceHandle, Constants.WM_CAP_DRIVER_DISCONNECT, (IntPtr)deviceNumber, (IntPtr)0);
  167.                     User32.DestroyWindow(deviceHandle);
  168.                 }
  169.                 deviceHandle = new IntPtr(0);
  170.  
  171.             }
  172.  
  173.             /// <summary>
  174.             /// Returns a captured image
  175.             /// </summary>
  176.             /// <returns>an image, null if capture failed</returns>
  177.             public BitmapSource Capture()
  178.             {
  179.                 if (deviceHandle.ToInt32() != 0)
  180.                 {
  181.                     User32.SendMessage(deviceHandle, Constants.WM_CAP_EDIT_COPY, (IntPtr)0, (IntPtr)0);
  182.  
  183.                     BitmapSource bs = Clipboard.GetImage();
  184.                     return bs;
  185.  
  186.                     /*
  187.                     IDataObject ido = Clipboard.GetDataObject();
  188.                     if (ido.GetDataPresent(DataFormats.Bitmap))
  189.                     {
  190.                         Bitmap bmp = ((Bitmap)ido.GetData(DataFormats.Bitmap));
  191.                         return ((Bitmap)ido.GetData(DataFormats.Bitmap));
  192.                     }
  193.                      */
  194.                 }
  195.  
  196.                 return null;
  197.             }
  198.  
  199.             /// <summary>
  200.             /// Returns an array with available capture devices
  201.             /// </summary>
  202.             /// <returns>the device names</returns>
  203.             public static List<CaptureDevice> GetDevices()
  204.             {
  205.                 List<CaptureDevice> devices = new List<CaptureDevice>();
  206.  
  207.                 for (ushort i = 0; i < MAX_DEVICES; ++i)
  208.                 {
  209.                     int capacity = 200;
  210.                     StringBuilder name = new StringBuilder(capacity);
  211.                     StringBuilder description = new StringBuilder(capacity);
  212.  
  213.                     if (Avicap32.capGetDriverDescription(i, name, capacity, description, capacity).ToInt32() > 0)
  214.                     {
  215.                         devices.Add(new CaptureDevice(i, name.ToString(), description.ToString()));
  216.                     }
  217.                 }
  218.  
  219.                 return devices;
  220.             }
  221.         }
  222. }

Attachment

Strumenti personali
hobby
approfondimenti