Advertisement
Guest User

webcam

a guest
Jun 10th, 2009
2,450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.61 KB | None | 0 0
  1. public void DisplayVideoCaptureDevicePropertyPage(IntPtr handle)
  2.         {
  3.             IBaseFilter device = CreateFilter(DirectShowLib.FilterCategory.VideoInputDevice, "myDeviceName");
  4.             DisplayPropertyPage(device, handle);
  5.         }
  6.  
  7.         #endregion
  8.  
  9.         #region private methods
  10.  
  11.         private IBaseFilter CreateFilter(Guid category, string friendlyname)
  12.         {
  13.             object source = null;
  14.             Guid iid = typeof(IBaseFilter).GUID;
  15.             foreach (DsDevice device in DsDevice.GetDevicesOfCat(category))
  16.             {
  17.                 if (device.Name.CompareTo(friendlyname) == 0)
  18.                 {
  19.                     device.Mon.BindToObject(null, null, ref iid, out source);
  20.                     break;
  21.                 }
  22.             }
  23.  
  24.             return (IBaseFilter)source;
  25.         }
  26.  
  27.         private void DisplayPropertyPage(IBaseFilter dev, IntPtr handle)
  28.         {
  29.             //Get the ISpecifyPropertyPages for the filter
  30.             ISpecifyPropertyPages pProp = dev as ISpecifyPropertyPages;
  31.             int hr = 0;
  32.  
  33.             if (pProp == null)
  34.             {
  35.                 //If the filter doesn't implement ISpecifyPropertyPages, try displaying IAMVfwCompressDialogs instead!
  36.                 IAMVfwCompressDialogs compressDialog = dev as IAMVfwCompressDialogs;
  37.                 if (compressDialog != null)
  38.                 {
  39.  
  40.                     hr = compressDialog.ShowDialog(VfwCompressDialogs.Config, IntPtr.Zero);
  41.                     DsError.ThrowExceptionForHR(hr);
  42.                 }
  43.                 return;
  44.             }
  45.  
  46.             //Get the name of the filter from the FilterInfo struct
  47.             DirectShowLib.FilterInfo filterInfo;
  48.             hr = dev.QueryFilterInfo(out filterInfo);
  49.             DsError.ThrowExceptionForHR(hr);
  50.  
  51.             // Get the propertypages from the property bag
  52.             DsCAUUID caGUID;
  53.             hr = pProp.GetPages(out caGUID);
  54.             DsError.ThrowExceptionForHR(hr);
  55.  
  56.             // Check for property pages on the output pin
  57.             IPin pPin = DsFindPin.ByDirection(dev, PinDirection.Output, 0);
  58.             ISpecifyPropertyPages pProp2 = pPin as ISpecifyPropertyPages;
  59.             if (pProp2 != null)
  60.             {
  61.                 DsCAUUID caGUID2;
  62.                 hr = pProp2.GetPages(out caGUID2);
  63.                 DsError.ThrowExceptionForHR(hr);
  64.  
  65.                 if (caGUID2.cElems > 0)
  66.                 {
  67.                     int soGuid = Marshal.SizeOf(typeof(Guid));
  68.  
  69.                     // Create a new buffer to hold all the GUIDs
  70.                     IntPtr p1 = Marshal.AllocCoTaskMem((caGUID.cElems + caGUID2.cElems) * soGuid);
  71.  
  72.                     // Copy over the pages from the Filter
  73.                     for (int x = 0; x < caGUID.cElems * soGuid; x++)
  74.                     {
  75.                         Marshal.WriteByte(p1, x, Marshal.ReadByte(caGUID.pElems, x));
  76.                     }
  77.  
  78.                     // Add the pages from the pin
  79.                     for (int x = 0; x < caGUID2.cElems * soGuid; x++)
  80.                     {
  81.                         Marshal.WriteByte(p1, x + (caGUID.cElems * soGuid), Marshal.ReadByte(caGUID2.pElems, x));
  82.                     }
  83.  
  84.                     // Release the old memory
  85.                     Marshal.FreeCoTaskMem(caGUID.pElems);
  86.                     Marshal.FreeCoTaskMem(caGUID2.pElems);
  87.  
  88.                     // Reset caGUID to include both
  89.                     caGUID.pElems = p1;
  90.                     caGUID.cElems += caGUID2.cElems;
  91.                 }
  92.             }
  93.  
  94.             // Create and display the OlePropertyFrame
  95.             object oDevice = (object)dev;
  96.             hr = OleCreatePropertyFrame(handle, 0, 0, filterInfo.achName, 1, ref oDevice, caGUID.cElems, caGUID.pElems, 0, 0, IntPtr.Zero);
  97.             DsError.ThrowExceptionForHR(hr);
  98.  
  99.             // Release COM objects
  100.             Marshal.FreeCoTaskMem(caGUID.pElems);
  101.             Marshal.ReleaseComObject(pProp);
  102.             if (filterInfo.pGraph != null)
  103.             {
  104.                 Marshal.ReleaseComObject(filterInfo.pGraph);
  105.             }
  106.         }
  107.  
  108.         [DllImport(@"oleaut32.dll")]
  109.         public static extern int OleCreatePropertyFrame(
  110.             IntPtr hwndOwner,
  111.             int x,
  112.             int y,
  113.             [MarshalAs(UnmanagedType.LPWStr)] string lpszCaption,
  114.             int cObjects,
  115.             [MarshalAs(UnmanagedType.Interface, ArraySubType = UnmanagedType.IUnknown)]
  116.             ref object ppUnk,
  117.             int cPages,
  118.             IntPtr lpPageClsID,
  119.             int lcid,
  120.             int dwReserved,
  121.             IntPtr lpvReserved);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement