Winforms add in to connect to USB camera?

Bradley Rogers 116 Reputation points
2021-03-13T20:55:56.693+00:00

For 2 days have searched for any way to include a usb camera into a C# app in VS2019 with win Forms. Ive loaded 5 or ? packages like OpenCV and add ins for cameras, chased a project that uses the laptops web cam? NO, Not the build in camera, this has to open the usb and find the camera, connect then accept video or take snapshots. Its for a Microsoft ML project Should be simple, add in ??? then create a panel or ? in a form designer add a button and on click say initialize the usb and find the camera, another button to connect and stream video to the panel or ? This feature has got to be around, probably even archived because who uses Forms anymore? most everything is web. It doesnt matter which version of .net its a new app and has to make use of ML.Microsoft at some point, hopefully today Had the Microsoft HD-3000 usb camera on my porch for a few minutes today until it was stolen, the irony?

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,821 questions
Windows Hardware Performance
Windows Hardware Performance
Windows: A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.Hardware Performance: Delivering / providing hardware or hardware systems or adjusting / adapting hardware or hardware systems.
1,541 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Bradley Rogers 116 Reputation points
    2021-03-13T20:57:53.747+00:00

    Goal: either nuget or other add in to allow me to code up an app to stream video from some generic usb camera hopefully in c#

    also am going to make a project in ML.Microsoft and the vs2019 community only seems to support that in F# right now?

    0 comments No comments

  2. Castorix31 81,461 Reputation points
    2021-03-13T21:22:28.737+00:00

    You can use DirectShow
    I had posted a sample in VB in this thread for a Camera preview test
    (same code in C#)


  3. Bradley Rogers 116 Reputation points
    2021-03-14T01:40:49.447+00:00

    Hi
    thanks for the fast answer, i tried to convert to c# but has 48 errors still, cant get past it, did you have a version in c#?

    does it have to be done in vb? as the rest of the project is c#
    thanks!

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Security;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.VisualBasic;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace WinFormsApp2
    {
    
    
    
    
    
        public partial class Form1
        {
            public enum HRESULT : uint
            {
                S_OK = 0,
                S_FALSE = 1,
                E_NOTIMPL = 0x80004001,
                E_NOINTERFACE = 0x80004002,
                E_POINTER = 0x80004003,
                E_FAIL = 0x80004005,
                E_UNEXPECTED = 0x8000FFFF,
                E_OUTOFMEMORY = 0x8007000E
            }
    
            [ComImport()]
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            [Guid("56a868a9-0ad4-11ce-b03a-0020af0ba770")]
            interface IGraphBuilder : IFilterGraph
            {
                new HRESULT AddFilter(IBaseFilter pFilter, string pName);
                new HRESULT RemoveFilter(IBaseFilter pFilter);
                new HRESULT EnumFilters(out IEnumFilters ppEnum);
                new HRESULT FindFilterByName(string pName, out IBaseFilter ppFilter);
                new HRESULT ConnectDirect(IPin ppinOut, IPin ppinIn, IntPtr pmt);
                new HRESULT Reconnect(IPin ppin);
                new HRESULT Disconnect(IPin ppin);
                new HRESULT SetDefaultSyncSource();
                HRESULT Connect(IPin ppinOut, IPin ppinIn);
                HRESULT Render(IPin ppinOut);
                HRESULT RenderFile(string lpcwstrFile, string lpcwstrPlayList);
                HRESULT AddSourceFilter(string lpcwstrFileName, string lpcwstrFilterName, out IBaseFilter ppFilter);
                HRESULT SetLogFile(IntPtr hFile);
                HRESULT Abort();
                HRESULT ShouldOperationContinue();
            }
    
            [ComImport()]
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            [Guid("36b73882-c2c8-11cf-8b46-00805f6cef60")]
            interface IFilterGraph2 : IGraphBuilder
            {
                new HRESULT AddFilter(IBaseFilter pFilter, string pName);
                new HRESULT RemoveFilter(IBaseFilter pFilter);
                new HRESULT EnumFilters(out IEnumFilters ppEnum);
                new HRESULT FindFilterByName(string pName, out IBaseFilter ppFilter);
                new HRESULT ConnectDirect(IPin ppinOut, IPin ppinIn, IntPtr pmt);
                new HRESULT Reconnect(IPin ppin);
                new HRESULT Disconnect(IPin ppin);
                new HRESULT SetDefaultSyncSource();
                new HRESULT Connect(IPin ppinOut, IPin ppinIn);
                new HRESULT Render(IPin ppinOut);
                new HRESULT RenderFile(string lpcwstrFile, string lpcwstrPlayList);
                new HRESULT AddSourceFilter(string lpcwstrFileName, string lpcwstrFilterName, out IBaseFilter ppFilter);
                new HRESULT SetLogFile(IntPtr hFile);
                new HRESULT Abort();
                new HRESULT ShouldOperationContinue();
                HRESULT AddSourceFilterForMoniker(IMoniker pMoniker, IntPtr pCtx, string lpcwstrFilterName, out IBaseFilter ppFilter);
                HRESULT ReconnectEx(IPin ppin, AM_MEDIA_TYPE pmt);
                HRESULT RenderEx(IPin pPinOut, uint dwFlags, ref uint pvContext);
            }
    
            public enum AM_RENSDEREXFLAGS
            {
                AM_RENDEREX_RENDERTOEXISTINGRENDERERS = 0x1
            }
    
            [ComImport()]
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            [Guid("56a8689f-0ad4-11ce-b03a-0020af0ba770")]
            interface IFilterGraph
            {
                HRESULT AddFilter(IBaseFilter pFilter, string pName);
                HRESULT RemoveFilter(IBaseFilter pFilter);
                HRESULT EnumFilters(out IEnumFilters ppEnum);
                HRESULT FindFilterByName(string pName, out IBaseFilter ppFilter);
                HRESULT ConnectDirect(IPin ppinOut, IPin ppinIn, IntPtr pmt);
                HRESULT Reconnect(IPin ppin);
                HRESULT Disconnect(IPin ppin);
                HRESULT SetDefaultSyncSource();
            }
    
            [ComImport()]
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            [Guid("56a86893-0ad4-11ce-b03a-0020af0ba770")]
            interface IEnumFilters
            {
                HRESULT Next(uint cFilters, out IBaseFilter ppFilter, out uint pcFetched);
                HRESULT Skip(uint cFilters);
                HRESULT Reset();
                HRESULT Clone(out IEnumFilters ppEnum);
            }
    
            [ComImport()]
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            [Guid("0000010c-0000-0000-C000-000000000046")]
            interface IPersist
            {
                HRESULT GetClassID(out Guid pClassID);
            }
    
            [ComImport()]
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            [Guid("56a86899-0ad4-11ce-b03a-0020af0ba770")]
            interface IMediaFilter : IPersist
            {
                new HRESULT GetClassID(out Guid pClassID);
                HRESULT Stop();
                HRESULT Pause();
                HRESULT Run(Int64 tStart);
                HRESULT GetState(int dwMilliSecsTimeout, out FILTER_STATE State);
                HRESULT SetSyncSource(IntPtr pClock);
                HRESULT GetSyncSource(out IntPtr pClock);
            }
    
            [ComImport()]
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            [Guid("56a86895-0ad4-11ce-b03a-0020af0ba770")]
            interface IBaseFilter : IMediaFilter
            {
                new HRESULT GetClassID(out Guid pClassID);
                new HRESULT Stop();
                new HRESULT Pause();
                new HRESULT Run(Int64 tStart);
                new HRESULT GetState(int dwMilliSecsTimeout, out FILTER_STATE State);
                new HRESULT SetSyncSource(IntPtr pClock);
                new HRESULT GetSyncSource(out IntPtr pClock);
                HRESULT EnumPins(out IEnumPins ppEnum);
                HRESULT FindPin(string Id, out IPin ppPin);
                HRESULT QueryFilterInfo(out FILTER_INFO pInfo);
                HRESULT JoinFilterGraph(IFilterGraph pGraph, string pName);
                HRESULT QueryVendorInfo(out string pVendorInfo);
            }
    
            [ComImport()]
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            [Guid("56a86892-0ad4-11ce-b03a-0020af0ba770")]
            interface IEnumPins
            {
                HRESULT Next(uint cPins, out IPin ppPins, out uint pcFetched);
                HRESULT Skip(uint cPins);
                HRESULT Reset();
                HRESULT Clone(out IEnumPins ppEnum);
            }
    
            [ComImport()]
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            [Guid("56a86891-0ad4-11ce-b03a-0020af0ba770")]
            interface IPin
            {
                HRESULT Connect(IPin pReceivePin, AM_MEDIA_TYPE pmt);
                HRESULT ReceiveConnection(IPin pConnector, AM_MEDIA_TYPE pmt);
                HRESULT Disconnect();
                HRESULT ConnectedTo(out IPin pPin);
                HRESULT ConnectionMediaType(out AM_MEDIA_TYPE pmt);
                HRESULT QueryPinInfo(out PIN_INFO pInfo);
                HRESULT QueryDirection(out PIN_DIRECTION pPinDir);
                HRESULT QueryId(out string Id);
                HRESULT QueryAccept(AM_MEDIA_TYPE pmt);
                HRESULT EnumMediaTypes(out IEnumMediaTypes ppEnum);
                HRESULT QueryInternalConnections(out IPin apPin, ref uint nPin);
                HRESULT EndOfStream();
                HRESULT BeginFlush();
                HRESULT EndFlush();
                HRESULT NewSegment(Int64 tStart, Int64 tStop, double dRate);
            }
    
            [ComImport()]
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            [Guid("89c31040-846b-11ce-97d3-00aa0055595a")]
            public interface IEnumMediaTypes
            {
                HRESULT Next(uint cMediaTypes, ref AM_MEDIA_TYPE ppMediaTypes, ref uint pcFetched);
                HRESULT Skip(uint cMediaTypes);
                HRESULT Reset();
                HRESULT Clone(ref IEnumMediaTypes ppEnum);
            }
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
            public struct AM_MEDIA_TYPE
            {
                public Guid majortype;
                public Guid subtype;
                public bool bFixedSizeSamples;
                public bool bTemporalCompression;
                public uint lSampleSize;
                public Guid formattype;
                // public pUnk As IUnknown
                public IntPtr pUnk;
                public uint cbFormat;
                public byte pbFormat;
            }
    
            public enum PIN_DIRECTION : int
            {
                PINDIR_INPUT = 0,
                PINDIR_OUTPUT = (PIN_DIRECTION.PINDIR_INPUT + 1)
            }
    
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
            public class PIN_INFO
            {
                public IBaseFilter filter;
                public PIN_DIRECTION dir;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
                public string name;
            }
    
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
            public class FILTER_INFO
            {
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
                public string achName;
                [MarshalAs(UnmanagedType.IUnknown)]
                private object pUnk;
            }
    
            public enum FILTER_STATE
            {
                State_Stopped = 0,
                State_Paused = (FILTER_STATE.State_Stopped + 1),
                State_Running = (FILTER_STATE.State_Paused + 1)
            }
    
            [ComImport()]
            [Guid("56A868B1-0AD4-11CE-B03A-0020AF0BA770")]
            [InterfaceType(ComInterfaceType.InterfaceIsDual)]
            public interface IMediaControl
            {
                HRESULT Run();
                HRESULT Pause();
                HRESULT Stop();
                HRESULT GetState(int msTimeout, ref int pfs);
                HRESULT RenderFile(string strFilename);
                HRESULT AddSourceFilter(string strFilename, ref object ppUnk);
                HRESULT get_FilterCollection(ref object ppUnk);
                HRESULT get_RegFilterCollection(ref object ppUnk);
                HRESULT StopWhenReady();
            }
    
            [ComImport()]
            [InterfaceType(ComInterfaceType.InterfaceIsDual)]
            [Guid("56a868b4-0ad4-11ce-b03a-0020af0ba770")]
            public interface IVideoWindow
            {
                HRESULT put_Caption(string strCaption);
                HRESULT get_Caption(ref string strCaption);
                HRESULT put_WindowStyle(int WindowStyle);
                HRESULT get_WindowStyle(ref int WindowStyle);
                HRESULT put_WindowStyleEx(int WindowStyleEx);
                HRESULT get_WindowStyleEx(ref int WindowStyleEx);
                HRESULT put_AutoShow(int AutoShow);
                HRESULT get_AutoShow(ref int AutoShow);
                HRESULT put_WindowState(int WindowState);
                HRESULT get_WindowState(ref int WindowState);
                HRESULT put_BackgroundPalette(int BackgroundPalette);
                HRESULT get_BackgroundPalette(ref int pBackgroundPalette);
                [PreserveSig()]
                HRESULT put_Visible(int Visible);
                HRESULT get_Visible(ref int pVisible);
                HRESULT put_Left(int Left);
                HRESULT get_Left(ref int pLeft);
                HRESULT put_Width(int Width);
                HRESULT get_Width(ref int pWidth);
                HRESULT put_Top(int Top);
                HRESULT get_Top(ref int pTop);
                HRESULT put_Height(int Height);
                HRESULT get_Height(ref int pHeight);
                [PreserveSig()]
                HRESULT put_Owner(IntPtr Owner);
                HRESULT get_Owner(ref IntPtr Owner);
                HRESULT put_MessageDrain(IntPtr Drain);
                HRESULT get_MessageDrain(ref IntPtr Drain);
                HRESULT get_BorderColor(ref int Color);
                HRESULT put_BorderColor(int Color);
                HRESULT get_FullScreenMode(ref int FullScreenMode);
                HRESULT put_FullScreenMode(int FullScreenMode);
                HRESULT SetWindowForeground(int Focus);
                HRESULT NotifyOwnerMessage(IntPtr hwnd, int uMsg, int wParam, IntPtr lParam);
                [PreserveSig()]
                HRESULT SetWindowPosition(int Left, int Top, int Width, int Height);
                HRESULT GetWindowPosition(ref int pLeft, ref int pTop, ref int pWidth, ref int pHeight);
                HRESULT GetMinIdealImageSize(ref int pWidth, ref int pHeight);
                HRESULT GetMaxIdealImageSize(ref int pWidth, ref int pHeight);
                HRESULT GetRestorePosition(ref int pLeft, ref int pTop, ref int pWidth, ref int pHeight);
                HRESULT HideCursor(int Hide);
                HRESULT IsCursorHidden(ref int CursorHidden);
            }
    
            [ComImport()]
            [InterfaceType(ComInterfaceType.InterfaceIsDual)]
            [Guid("56a868c0-0ad4-11ce-b03a-0020af0ba770")]
            public interface IMediaEventEx : IMediaEvent
            {
                new HRESULT GetEventHandle(ref IntPtr hEvent);
                [PreserveSig()]
                new HRESULT GetEvent(ref int lEventCode, ref IntPtr lParam1, ref IntPtr lParam2, int msTimeout);
                new HRESULT WaitForCompletion(int msTimeout, ref int pEvCode);
                new HRESULT CancelDefaultHandling(long lEvCode);
                new HRESULT RestoreDefaultHandling(int lEvCode);
                new HRESULT FreeEventParams(int lEvCode, IntPtr lParam1, IntPtr lParam2);
    
                HRESULT SetNotifyWindow(IntPtr hwnd, int lMsg, IntPtr lInstanceData);
                HRESULT SetNotifyFlags(int lNoNotifyFlags);
                HRESULT GetNotifyFlags(ref int lplNoNotifyFlags);
            }
    
            [ComImport()]
            [InterfaceType(ComInterfaceType.InterfaceIsDual)]
            [Guid("56a868b6-0ad4-11ce-b03a-0020af0ba770")]
            public interface IMediaEvent
            {
                HRESULT GetEventHandle(ref IntPtr hEvent);
                [PreserveSig()]
                HRESULT GetEvent(ref int lEventCode, ref IntPtr lParam1, ref IntPtr lParam2, int msTimeout);
                HRESULT WaitForCompletion(int msTimeout, ref int pEvCode);
                HRESULT CancelDefaultHandling(long lEvCode);
                HRESULT RestoreDefaultHandling(int lEvCode);
                HRESULT FreeEventParams(int lEvCode, IntPtr lParam1, IntPtr lParam2);
            }
    
            [ComImport()]
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            [Guid("93E5A4E0-2D50-11d2-ABFA-00A0C9C6E38D")]
            public interface ICaptureGraphBuilder2
            {
                HRESULT SetFiltergraph(IGraphBuilder pfg);
                HRESULT GetFiltergraph(ref IGraphBuilder ppfg);
                HRESULT SetOutputFileName(ref Guid pType, string lpstrFile, ref IBaseFilter ppf, ref IFileSinkFilter ppSink);
                HRESULT FindInterface(ref Guid pCategory, ref Guid pType, IBaseFilter pf, ref Guid riid, ref IntPtr ppint);
                [PreserveSig()]
                HRESULT RenderStream(ref Guid pCategory, ref Guid pType, IntPtr pSource, IBaseFilter pfCompressor, IBaseFilter pfRenderer);
                HRESULT ControlStream(ref Guid pCategory, ref Guid pType, IBaseFilter pFilter, Int64 pstart, Int64 pstop, UInt16 wStartCookie, UInt16 wStopCookie);
                HRESULT AllocCapFile(string lpstr, UInt64 dwlSize);
                HRESULT CopyCaptureFile(string lpwstrOld, string lpwstrNew, int fAllowEscAbort, IAMCopyCaptureFileProgress pCallback);
                HRESULT FindPin(IntPtr pSource, PIN_DIRECTION pindir, ref Guid pCategory, ref Guid pType, bool fUnconnected, int num, ref IPin ppPin);
            }
    
            [ComImport()]
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            [Guid("a2104830-7c70-11cf-8bce-00aa00a3f1a6")]
            public interface IFileSinkFilter
            {
                HRESULT SetFileName(string pszFileName, AM_MEDIA_TYPE pmt);
                HRESULT GetCurFile(ref string ppszFileName, ref AM_MEDIA_TYPE pmt);
            }
    
            [ComImport()]
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            [Guid("670d1d20-a068-11d0-b3f0-00aa003761c5")]
            public interface IAMCopyCaptureFileProgress
            {
                HRESULT Progress(int iProgress);
            }
    
            [ComImport()]
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            [Guid("29840822-5B84-11D0-BD3B-00A0C911CE86")]
            public interface ICreateDevEnum
            {
                HRESULT CreateClassEnumerator(ref Guid clsidDeviceClass, ref IEnumMoniker ppEnumMoniker, int dwFlags);
            }
    
            [ComImport()]
            [Guid("00000102-0000-0000-C000-000000000046")]
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            public interface IEnumMoniker
            {
                HRESULT Next(uint celt, ref IMoniker rgelt, ref uint pceltFetched);
                HRESULT Skip(uint celt);
                HRESULT Reset();
                HRESULT Clone(ref IEnumMoniker ppenum);
            }
    
            [ComImport()]
            [Guid("0000000f-0000-0000-C000-000000000046")]
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            public interface IMoniker : IPersistStream
            {
                new HRESULT GetClassID(ref Guid pClassID);
                new HRESULT IsDirty();
                new HRESULT Load(System.Runtime.InteropServices.ComTypes.IStream pStm);
                new HRESULT Save(System.Runtime.InteropServices.ComTypes.IStream pStm, bool fClearDirty);
                new HRESULT GetSizeMax(ref ULARGE_INTEGER pcbSize);
    
                HRESULT BindToObject(IntPtr pbc, IMoniker pmkToLeft, ref Guid riidResult, ref IntPtr ppvResult);
                HRESULT BindToStorage(IntPtr pbc, IMoniker pmkToLeft, ref Guid riid, ref IntPtr ppvObj);
                HRESULT Reduce(IntPtr pbc, int dwReduceHowFar, ref IMoniker ppmkToLeft, ref IMoniker ppmkReduced);
                HRESULT ComposeWith(IMoniker pmkRight, bool fOnlyIfNotGeneric, ref IMoniker ppmkComposite);
                HRESULT Enum(bool fForward, ref IEnumMoniker ppenumMoniker);
                HRESULT IsEqual(IMoniker pmkOtherMoniker);
                HRESULT Hash(ref int pdwHash);
                HRESULT IsRunning(IntPtr pbc, IMoniker pmkToLeft, IMoniker pmkNewlyRunning);
                HRESULT GetTimeOfLastChange(IntPtr pbc, IMoniker pmkToLeft, ref System.Runtime.InteropServices.ComTypes.FILETIME pFileTime);
                HRESULT Inverse(ref IMoniker ppmk);
                HRESULT CommonPrefixWith(IMoniker pmkOther, ref IMoniker ppmkPrefix);
                HRESULT RelativePathTo(IMoniker pmkOther, ref IMoniker ppmkRelPath);
                HRESULT GetDisplayName(IntPtr pbc, IMoniker pmkToLeft, ref string ppszDisplayName);
                HRESULT ParseDisplayName(IntPtr pbc, IMoniker pmkToLeft, string pszDisplayName, ref uint pchEaten, ref IMoniker ppmkOut);
                HRESULT IsSystemMoniker(ref int pdwMksys);
            }
    
            [ComImport()]
            [Guid("00000109-0000-0000-C000-000000000046")]
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            public interface IPersistStream : IPersist
            {
                new HRESULT GetClassID(ref Guid pClassID);
    
                HRESULT IsDirty();
                HRESULT Load(System.Runtime.InteropServices.ComTypes.IStream pStm);
                HRESULT Save(System.Runtime.InteropServices.ComTypes.IStream pStm, bool fClearDirty);
                HRESULT GetSizeMax(ref ULARGE_INTEGER pcbSize);
            }
    
            [StructLayout(LayoutKind.Explicit)]
            public struct ULARGE_INTEGER
            {
                [FieldOffset(0)]
                public int LowPart;
                [FieldOffset(4)]
                public int HighPart;
                [FieldOffset(0)]
                public long QuadPart;
            }
    
            [ComImport()]
            [Guid("55272A00-42CB-11CE-8135-00AA004BB851")]
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            public interface IPropertyBag
            {
                HRESULT Read(string pszPropName, ref PROPVARIANT pVar, IErrorLog pErrorLog);
                HRESULT Write(string pszPropName, IntPtr pVar);
            }
    
            [ComImport()]
            [Guid("3127CA40-446E-11CE-8135-00AA004BB851")]
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            public interface IErrorLog
            {
                HRESULT AddError(string pszPropName, System.Runtime.InteropServices.ComTypes.EXCEPINFO pExcepInfo);
            }
    
            [StructLayout(LayoutKind.Sequential)]
            public struct PROPARRAY
            {
                public uint cElems;
                public IntPtr pElems;
            }
    
            [StructLayout(LayoutKind.Explicit, Pack = 1)]
            public struct PROPVARIANT
            {
                [FieldOffset(0)]
                public System.UInt16 varType;
                [FieldOffset(2)]
                public System.UInt16 wReserved1;
                [FieldOffset(4)]
                public System.UInt16 wReserved2;
                [FieldOffset(6)]
                public System.UInt16 wReserved3;
                [FieldOffset(8)]
                public byte bVal;
                [FieldOffset(8)]
                public System.SByte cVal;
                [FieldOffset(8)]
                public System.UInt16 uiVal;
                [FieldOffset(8)]
                public short iVal;
                [FieldOffset(8)]
                public UInt32 uintVal;
                [FieldOffset(8)]
                public Int32 intVal;
                [FieldOffset(8)]
                public UInt64 ulVal;
                [FieldOffset(8)]
                public Int64 lVal;
                [FieldOffset(8)]
                public float fltVal;
                [FieldOffset(8)]
                public double dblVal;
                [FieldOffset(8)]
                public short boolVal;
                [FieldOffset(8)]
                public IntPtr pclsidVal;
                // GUID ID pointer
                [FieldOffset(8)]
                public IntPtr pszVal;
                // Ansi string pointer
                [FieldOffset(8)]
                public IntPtr pwszVal;
                // Unicode string pointer
                [FieldOffset(8)]
                public IntPtr punkVal;
                // punkVal (interface pointer)
                [FieldOffset(8)]
                public PROPARRAY ca;
                [FieldOffset(8)]
                public System.Runtime.InteropServices.ComTypes.FILETIME filetime;
            }
    
            public enum VARENUM
            {
                VT_EMPTY = 0,
                VT_NULL = 1,
                VT_I2 = 2,
                VT_I4 = 3,
                VT_R4 = 4,
                VT_R8 = 5,
                VT_CY = 6,
                VT_DATE = 7,
                VT_BSTR = 8,
                VT_DISPATCH = 9,
                VT_ERROR = 10,
                VT_BOOL = 11,
                VT_VARIANT = 12,
                VT_UNKNOWN = 13,
                VT_DECIMAL = 14,
                VT_I1 = 16,
                VT_UI1 = 17,
                VT_UI2 = 18,
                VT_UI4 = 19,
                VT_I8 = 20,
                VT_UI8 = 21,
                VT_INT = 22,
                VT_UINT = 23,
                VT_VOID = 24,
                VT_HRESULT = 25,
                VT_PTR = 26,
                VT_SAFEARRAY = 27,
                VT_CARRAY = 28,
                VT_USERDEFINED = 29,
                VT_LPSTR = 30,
                VT_LPWSTR = 31,
                VT_RECORD = 36,
                VT_INT_PTR = 37,
                VT_UINT_PTR = 38,
                VT_FILETIME = 64,
                VT_BLOB = 65,
                VT_STREAM = 66,
                VT_STORAGE = 67,
                VT_STREAMED_OBJECT = 68,
                VT_STORED_OBJECT = 69,
                VT_BLOB_OBJECT = 70,
                VT_CF = 71,
                VT_CLSID = 72,
                VT_VERSIONED_STREAM = 73,
                VT_BSTR_BLOB = 0xFFF,
                VT_VECTOR = 0x1000,
                VT_ARRAY = 0x2000,
                VT_BYREF = 0x4000,
                VT_RESERVED = 0x8000,
                VT_ILLEGAL = 0xFFF,
                VT_ILLEGALMASKED = 0xFFF,
                VT_TYPEMASK = 0xFFF
            }
    
            [ComImport()]
            [Guid("5a804648-4f66-4867-9c43-4f5c822cf1b8")]
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            public interface IVMRFilterConfig9
            {
                HRESULT SetImageCompositor(IVMRImageCompositor9 lpVMRImgCompositor);
                HRESULT SetNumberOfStreams(uint dwMaxStreams);
                HRESULT GetNumberOfStreams(ref uint pdwMaxStreams);
                HRESULT SetRenderingPrefs(uint dwRenderFlags);
                HRESULT GetRenderingPrefs(ref uint pdwRenderFlags);
                HRESULT SetRenderingMode(uint Mode);
                HRESULT GetRenderingMode(ref uint pMode);
            }
    
            [ComImport()]
            [Guid("4a5c89eb-df51-4654-ac2a-e48e02bbabf6")]
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            public interface IVMRImageCompositor9
            {
                HRESULT InitCompositionDevice(IntPtr pD3DDevice);
                HRESULT TermCompositionDevice(IntPtr pD3DDevice);
                HRESULT SetStreamMediaType(uint dwStrmID, AM_MEDIA_TYPE pmt, bool fTexture);
                HRESULT CompositeImage(IntPtr pD3DDevice, IntPtr pddsRenderTarget, AM_MEDIA_TYPE pmtRenderTarget, long rtStart, long rtEnd, uint dwClrBkGnd, ref VMR9VideoStreamInfo pVideoStreamInfo, uint cStreams);
            }
    
            [StructLayout(LayoutKind.Sequential)]
            public struct VMR9VideoStreamInfo
            {
                public IntPtr pddsVideoSurface;
                public uint dwWidth;
                public uint dwHeight;
                public uint dwStrmID;
                public float fAlpha;
                private VMR9NormalizedRect rNormal;
                private long rtStart;
                private long rtEnd;
                private VMR9_SampleFormat SampleFormat;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            public struct VMR9NormalizedRect
            {
                public float left;
                public float top;
                public float right;
                public float bottom;
            }
    
            public enum VMR9_SampleFormat
            {
                VMR9_SampleReserved = 1,
                VMR9_SampleProgressiveFrame = 2,
                VMR9_SampleFieldInterleavedEvenFirst = 3,
                VMR9_SampleFieldInterleavedOddFirst = 4,
                VMR9_SampleFieldSingleEven = 5,
                VMR9_SampleFieldSingleOdd = 6
            }
    
            public enum VMR9Mode
            {
                VMR9Mode_Windowed = 1,
                VMR9Mode_Windowless = 2,
                VMR9Mode_Renderless = 4,
                VMR9Mode_Mask = 7
            }
    
            [ComImport()]
            [Guid("8f537d09-f85e-4414-b23b-502e54c79927")]
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            public interface IVMRWindowlessControl9
            {
                HRESULT GetNativeVideoSize(ref int lpWidth, ref int lpHeight, ref int lpARWidth, ref int lpARHeight);
                HRESULT GetMinIdealVideoSize(ref int lpWidth, ref int lpHeight);
                HRESULT GetMaxIdealVideoSize(ref int lpWidth, ref int lpHeight);
                HRESULT SetVideoPosition(IntPtr lpSRCRect, ref RECT lpDSTRect);
                HRESULT GetVideoPosition(ref RECT lpSRCRect, ref RECT lpDSTRect);
                HRESULT GetAspectRatioMode(ref uint lpAspectRatioMode);
                HRESULT SetAspectRatioMode(uint AspectRatioMode);
                HRESULT SetVideoClippingWindow(IntPtr hwnd);
                HRESULT RepaintVideo(IntPtr hwnd, IntPtr hdc);
                HRESULT DisplayModeChanged();
                HRESULT GetCurrentImage(ref IntPtr lpDib);
                HRESULT SetBorderColor(uint Clr);
                HRESULT GetBorderColor(ref uint lpClr);
            }
    
            [ComImport()]
            [System.Security.SuppressUnmanagedCodeSecurity()]
            [Guid("ced175e5-1935-4820-81bd-ff6ad00c9108")]
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            public interface IVMRMixerBitmap9
            {
                [PreserveSig()]
                HRESULT SetAlphaBitmap(ref VMR9AlphaBitmap pBmpParms);
                [PreserveSig()]
                HRESULT UpdateAlphaBitmapParameters(ref VMR9AlphaBitmap pBmpParms);
                [PreserveSig()]
                HRESULT GetAlphaBitmapParameters(ref VMR9AlphaBitmap pBmpParms);
            }
    
            [StructLayout(LayoutKind.Sequential)]
            public struct VMR9AlphaBitmap
            {
                public uint dwFlags;
                public IntPtr hdc;
                public IntPtr pDDS;
                public RECT rSrc;
                public VMR9NormalizedRect rDest;
                public float fAlpha;
                public int clrSrcKey;
                public uint dwFilterMode;
            }
    
            private int VMRBITMAP_DISABLE = 1;
            private int VMRBITMAP_HDC = 2;
            private int VMRBITMAP_ENTIREDDS = 4;
            private int VMRBITMAP_SRCCOLORKEY = 8;
            private int VMRBITMAP_SRCRECT = 16;
    
            [StructLayout(LayoutKind.Sequential)]
            public struct RECT
            {
                public int left;
                public int top;
                public int right;
                public int bottom;
                public RECT(int left, int top, int right, int bottom)
                {
                    this.left = left;
                    this.top = top;
                    this.right = right;
                    this.bottom = bottom;
                }
            }
    
            private enum PLAYSTATE
            {
                Stopped,
                Paused,
                Running,
                Init
            }
    
            [DllImport("User32.dll", SetLastError = true)]
            public static IntPtr GetDC(IntPtr hWnd)
            {
            }
    
            [DllImport("User32.dll", SetLastError = true)]
            public static IntPtr ReleaseDC(IntPtr hWnd, IntPtr hdc)
            {
            }
    
            [DllImport("Gdi32.dll", SetLastError = true)]
            public static IntPtr CreateCompatibleDC(IntPtr hDC)
            {
            }
    
            [DllImport("Gdi32.dll", SetLastError = true)]
            public static IntPtr SelectObject(IntPtr hDC, IntPtr hObject)
            {
    
    0 comments No comments

  4. Castorix31 81,461 Reputation points
    2021-03-14T08:47:17.507+00:00

    I found a C# test in my archives
    (I just re-tested it on Windows 10) =>
    (change the namespace name...)

    using System;  
    using System.Collections.Generic;  
    using System.ComponentModel;  
    using System.Data;  
    using System.Drawing;  
    using System.Linq;  
    using System.Runtime.InteropServices;  
    using System.Text;  
    using System.Threading.Tasks;  
    using System.Windows.Forms;  
      
    // other Video Capture sources :  
    // E:\Sources\Windows-classic-samples-master\Samples\CaptureEngineVideoCapture  
    // E:\Sources\Windows-classic-samples-master\Samples\Win7Samples\multimedia\mediafoundation\SimpleCapture  
      
      
    // https://learn.microsoft.com/en-us/windows/win32/directshow/capturing-an-image-from-a-still-image-pin  
      
    // https://learn.microsoft.com/en-us/windows/win32/wia/-wia-capturing-a-still-image-from-streaming-video?redirectedfrom=MSDN  
      
      
    namespace CSharp_DirectShow_Capture  
    {  
        // Reference : https://github.com/pauldotknopf/WindowsSDK7-Samples/tree/master/multimedia/directshow/capture/playcap  
      
        public partial class Form1 : Form  
        {  
            public enum HRESULT : int  
            {  
                S_OK = 0,  
                S_FALSE = 1,  
                E_NOTIMPL = unchecked((int)0x80004001),  
                E_NOINTERFACE = unchecked((int)0x80004002),  
                E_POINTER = unchecked((int)0x80004003),  
                E_FAIL = unchecked((int)0x80004005),  
                E_UNEXPECTED = unchecked((int)0x8000FFFF),  
                E_OUTOFMEMORY = unchecked((int)0x8007000E),  
            }  
      
            [ComImport()]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            [Guid("56a868a9-0ad4-11ce-b03a-0020af0ba770")]  
            public interface IGraphBuilder : IFilterGraph  
            {  
                #region IFilterGraph  
                new HRESULT AddFilter(IBaseFilter pFilter, string pName);  
                new HRESULT RemoveFilter(IBaseFilter pFilter);  
                new HRESULT EnumFilters(out IEnumFilters ppEnum);  
                new HRESULT FindFilterByName(string pName, out IBaseFilter ppFilter);  
                //new HRESULT ConnectDirect(IPin ppinOut, IPin ppinIn, AM_MEDIA_TYPE pmt);  
                new HRESULT ConnectDirect(IPin ppinOut, IPin ppinIn, IntPtr pmt);  
                new HRESULT Reconnect(IPin ppin);  
                new HRESULT Disconnect(IPin ppin);  
                new HRESULT SetDefaultSyncSource();  
                #endregion  
      
                HRESULT Connect(IPin ppinOut, IPin ppinIn);  
                HRESULT Render(IPin ppinOut);  
                HRESULT RenderFile(string lpcwstrFile, string lpcwstrPlayList);  
                HRESULT AddSourceFilter(string lpcwstrFileName, string lpcwstrFilterName, out IBaseFilter ppFilter);  
                HRESULT SetLogFile(IntPtr hFile);  
                HRESULT Abort();  
                HRESULT ShouldOperationContinue();  
            }  
      
            [ComImport()]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            [Guid("36b73882-c2c8-11cf-8b46-00805f6cef60")]  
            public interface IFilterGraph2 : IGraphBuilder  
            {  
                #region IGraphBuilder  
                #region IFilterGraph  
                new HRESULT AddFilter(IBaseFilter pFilter, string pName);  
                new HRESULT RemoveFilter(IBaseFilter pFilter);  
                new HRESULT EnumFilters(out IEnumFilters ppEnum);  
                new HRESULT FindFilterByName(string pName, out IBaseFilter ppFilter);  
                //new HRESULT ConnectDirect(IPin ppinOut, IPin ppinIn, AM_MEDIA_TYPE pmt);  
                new HRESULT ConnectDirect(IPin ppinOut, IPin ppinIn, IntPtr pmt);  
                new HRESULT Reconnect(IPin ppin);  
                new HRESULT Disconnect(IPin ppin);  
                new HRESULT SetDefaultSyncSource();  
                #endregion  
      
                new HRESULT Connect(IPin ppinOut, IPin ppinIn);  
                new HRESULT Render(IPin ppinOut);  
                new HRESULT RenderFile(string lpcwstrFile, string lpcwstrPlayList);  
                new HRESULT AddSourceFilter(string lpcwstrFileName, string lpcwstrFilterName, out IBaseFilter ppFilter);  
                new HRESULT SetLogFile(IntPtr hFile);  
                new HRESULT Abort();  
                new HRESULT ShouldOperationContinue();  
                #endregion  
      
                //HRESULT AddSourceFilterForMoniker(IMoniker pMoniker, IBindCtx pCtx, string lpcwstrFilterName, out IBaseFilter ppFilter);  
                HRESULT AddSourceFilterForMoniker(IMoniker pMoniker, IntPtr pCtx, string lpcwstrFilterName, out IBaseFilter ppFilter);  
                HRESULT ReconnectEx(IPin ppin, AM_MEDIA_TYPE pmt);  
                HRESULT RenderEx(IPin pPinOut, uint dwFlags, ref uint pvContext);  
            }  
      
            public enum AM_RENSDEREXFLAGS  
            {  
                AM_RENDEREX_RENDERTOEXISTINGRENDERERS = 0x1  
            };  
      
            [ComImport()]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            [Guid("56a8689f-0ad4-11ce-b03a-0020af0ba770")]  
            public interface IFilterGraph  
            {  
                HRESULT AddFilter(IBaseFilter pFilter, string pName);  
                HRESULT RemoveFilter(IBaseFilter pFilter);  
                HRESULT EnumFilters(out IEnumFilters ppEnum);  
                HRESULT FindFilterByName(string pName, out IBaseFilter ppFilter);  
                //HRESULT ConnectDirect(IPin ppinOut, IPin ppinIn, AM_MEDIA_TYPE pmt);  
                HRESULT ConnectDirect(IPin ppinOut, IPin ppinIn, IntPtr pmt);  
                HRESULT Reconnect(IPin ppin);  
                HRESULT Disconnect(IPin ppin);  
                HRESULT SetDefaultSyncSource();  
            }  
      
            [ComImport()]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            [Guid("56a86893-0ad4-11ce-b03a-0020af0ba770")]  
            public interface IEnumFilters  
            {  
                HRESULT Next(uint cFilters, out IBaseFilter ppFilter, out uint pcFetched);  
                HRESULT Skip(uint cFilters);  
                HRESULT Reset();  
                HRESULT Clone(out IEnumFilters ppEnum);  
            }  
      
            [ComImport()]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            [Guid("0000010c-0000-0000-C000-000000000046")]  
            public interface IPersist  
            {  
                HRESULT GetClassID(out Guid pClassID);  
            }  
      
            [ComImport()]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            [Guid("56a86899-0ad4-11ce-b03a-0020af0ba770")]  
            public interface IMediaFilter : IPersist  
            {  
                #region IPersist  
                new HRESULT GetClassID(out Guid pClassID);  
                #endregion  
                HRESULT Stop();  
                HRESULT Pause();  
                HRESULT Run(Int64 tStart);  
                HRESULT GetState(int dwMilliSecsTimeout, out FILTER_STATE State);  
                HRESULT SetSyncSource(IntPtr pClock);  
                HRESULT GetSyncSource(out IntPtr pClock);  
                //HRESULT SetSyncSource(IReferenceClock pClock);  
                //HRESULT GetSyncSource(out IReferenceClock pClock);  
            }  
      
            [ComImport()]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            [Guid("56a86895-0ad4-11ce-b03a-0020af0ba770")]  
            public interface IBaseFilter : IMediaFilter  
            {  
                #region IMediaFilter  
                #region IPersist  
                new HRESULT GetClassID(out Guid pClassID);  
                #endregion  
                new HRESULT Stop();  
                new HRESULT Pause();  
                new HRESULT Run(Int64 tStart);  
                new HRESULT GetState(int dwMilliSecsTimeout, out FILTER_STATE State);  
                new HRESULT SetSyncSource(IntPtr pClock);  
                new HRESULT GetSyncSource(out IntPtr pClock);  
                #endregion  
      
                HRESULT EnumPins(out IEnumPins ppEnum);  
                HRESULT FindPin(string Id, out IPin ppPin);  
                HRESULT QueryFilterInfo(out FILTER_INFO pInfo);  
                HRESULT JoinFilterGraph(IFilterGraph pGraph, string pName);  
                HRESULT QueryVendorInfo(out string pVendorInfo);  
            }  
      
            [ComImport()]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            [Guid("56a86892-0ad4-11ce-b03a-0020af0ba770")]  
            public interface IEnumPins  
            {  
                HRESULT Next(uint cPins, out IPin ppPins, out uint pcFetched);  
                HRESULT Skip(uint cPins);  
                HRESULT Reset();  
                HRESULT Clone(out IEnumPins ppEnum);  
            }  
      
            [ComImport()]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            [Guid("56a86891-0ad4-11ce-b03a-0020af0ba770")]  
            public interface IPin  
            {  
                HRESULT Connect(IPin pReceivePin, AM_MEDIA_TYPE pmt);  
                HRESULT ReceiveConnection(IPin pConnector, AM_MEDIA_TYPE pmt);  
                HRESULT Disconnect();  
                HRESULT ConnectedTo(out IPin pPin);  
                HRESULT ConnectionMediaType(out AM_MEDIA_TYPE pmt);  
                HRESULT QueryPinInfo(out PIN_INFO pInfo);  
                HRESULT QueryDirection(out PIN_DIRECTION pPinDir);  
                HRESULT QueryId(out string Id);  
                HRESULT QueryAccept(AM_MEDIA_TYPE pmt);  
                HRESULT EnumMediaTypes(out IEnumMediaTypes ppEnum);  
                HRESULT QueryInternalConnections(out IPin apPin, ref uint nPin);  
                HRESULT EndOfStream();  
                HRESULT BeginFlush();  
                HRESULT EndFlush();  
                HRESULT NewSegment(Int64 tStart, Int64 tStop, double dRate);  
            }  
      
            [ComImport()]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            [Guid("89c31040-846b-11ce-97d3-00aa0055595a")]  
            public interface IEnumMediaTypes  
            {  
                HRESULT Next(uint cMediaTypes, out AM_MEDIA_TYPE ppMediaTypes, out uint pcFetched);  
                HRESULT Skip(uint cMediaTypes);  
                HRESULT Reset();  
                HRESULT Clone(out IEnumMediaTypes ppEnum);  
            }  
      
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]  
            public struct AM_MEDIA_TYPE  
            {  
                public Guid majortype;  
                public Guid subtype;  
                public bool bFixedSizeSamples;  
                public bool bTemporalCompression;  
                public uint lSampleSize;  
                public Guid formattype;  
                // public pUnk As IUnknown  
                public IntPtr pUnk;  
                public uint cbFormat;  
                public byte pbFormat;  
            }  
      
            public enum PIN_DIRECTION : int  
            {  
                PINDIR_INPUT = 0,  
                PINDIR_OUTPUT = (PINDIR_INPUT + 1)  
            }  
      
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]  
            public class PIN_INFO  
            {  
                public IBaseFilter filter;  
                public PIN_DIRECTION dir;  
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]  
                public string name;  
            }  
      
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]  
            public class FILTER_INFO  
            {  
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]  
                public string achName;  
                [MarshalAs(UnmanagedType.IUnknown)] // IFilterGraph  
                public object pUnk;  
            }  
      
            public enum FILTER_STATE  
            {  
                State_Stopped = 0,  
                State_Paused = (State_Stopped + 1),  
                State_Running = (State_Paused + 1)  
            }  
      
            [ComImport()]  
            [Guid("56A868B1-0AD4-11CE-B03A-0020AF0BA770")]  
            [InterfaceType(ComInterfaceType.InterfaceIsDual)]  
            public interface IMediaControl  
            {  
                HRESULT Run();  
                HRESULT Pause();  
                HRESULT Stop();  
                HRESULT GetState(int msTimeout, out int pfs);  
                HRESULT RenderFile(string strFilename);  
                HRESULT AddSourceFilter(string strFilename, out object ppUnk);  
                HRESULT get_FilterCollection(out object ppUnk);  
                HRESULT get_RegFilterCollection(out object ppUnk);  
                //HRESULT AddSourceFilter(string strFilename, out IDispatch ppUnk);  
                //HRESULT get_FilterCollection(out IDispatch ppUnk);  
                //HRESULT get_RegFilterCollection(out IDispatch ppUnk);  
                HRESULT StopWhenReady();  
            }  
      
            [ComImport()]  
            [InterfaceType(ComInterfaceType.InterfaceIsDual)]  
            [Guid("56a868b4-0ad4-11ce-b03a-0020af0ba770")]  
            public interface IVideoWindow  
            {  
                HRESULT put_Caption(string strCaption);  
                HRESULT get_Caption(out string strCaption);  
                HRESULT put_WindowStyle(int WindowStyle);  
                HRESULT get_WindowStyle(out int WindowStyle);  
                HRESULT put_WindowStyleEx(int WindowStyleEx);  
                HRESULT get_WindowStyleEx(out int WindowStyleEx);  
                HRESULT put_AutoShow(int AutoShow);  
                HRESULT get_AutoShow(out int AutoShow);  
                HRESULT put_WindowState(int WindowState);  
                HRESULT get_WindowState(out int WindowState);  
                HRESULT put_BackgroundPalette(int BackgroundPalette);  
                HRESULT get_BackgroundPalette(out int pBackgroundPalette);  
                [PreserveSig]  
                HRESULT put_Visible(int Visible);  
                HRESULT get_Visible(out int pVisible);  
                HRESULT put_Left(int Left);  
                HRESULT get_Left(out int pLeft);  
                HRESULT put_Width(int Width);  
                HRESULT get_Width(out int pWidth);  
                HRESULT put_Top(int Top);  
                HRESULT get_Top(out int pTop);  
                HRESULT put_Height(int Height);  
                HRESULT get_Height(out int pHeight);  
                [PreserveSig]  
                HRESULT put_Owner(IntPtr Owner);  
                HRESULT get_Owner(out IntPtr Owner);  
                HRESULT put_MessageDrain(IntPtr Drain);  
                HRESULT get_MessageDrain(out IntPtr Drain);  
                HRESULT get_BorderColor(out int Color);  
                HRESULT put_BorderColor(int Color);  
                HRESULT get_FullScreenMode(out int FullScreenMode);  
                HRESULT put_FullScreenMode(int FullScreenMode);  
                HRESULT SetWindowForeground(int Focus);  
                HRESULT NotifyOwnerMessage(IntPtr hwnd, int uMsg, int wParam, IntPtr lParam);  
                [PreserveSig]  
                HRESULT SetWindowPosition(int Left, int Top, int Width, int Height);  
                HRESULT GetWindowPosition(out int pLeft, out int pTop, out int pWidth, out int pHeight);  
                HRESULT GetMinIdealImageSize(out int pWidth, out int pHeight);  
                HRESULT GetMaxIdealImageSize(out int pWidth, out int pHeight);  
                HRESULT GetRestorePosition(out int pLeft, out int pTop, out int pWidth, out int pHeight);  
                HRESULT HideCursor(int HideCursor);  
                HRESULT IsCursorHidden(out int CursorHidden);  
            }  
      
            [ComImport()]  
            [InterfaceType(ComInterfaceType.InterfaceIsDual)]  
            [Guid("56a868c0-0ad4-11ce-b03a-0020af0ba770")]  
            public interface IMediaEventEx : IMediaEvent  
            {  
                #region IMediaEvent  
                new HRESULT GetEventHandle(out IntPtr hEvent);  
                [PreserveSig]  
                new HRESULT GetEvent(out int lEventCode, out IntPtr lParam1, out IntPtr lParam2, int msTimeout);  
                new HRESULT WaitForCompletion(int msTimeout, out int pEvCode);  
                new HRESULT CancelDefaultHandling(long lEvCode);  
                new HRESULT RestoreDefaultHandling(int lEvCode);  
                new HRESULT FreeEventParams(int lEvCode, IntPtr lParam1, IntPtr lParam2);  
                #endregion  
      
                HRESULT SetNotifyWindow(IntPtr hwnd, int lMsg, IntPtr lInstanceData);  
                HRESULT SetNotifyFlags(int lNoNotifyFlags);  
                HRESULT GetNotifyFlags(out int lplNoNotifyFlags);  
            }  
      
            [ComImport()]  
            [InterfaceType(ComInterfaceType.InterfaceIsDual)]  
            [Guid("56a868b6-0ad4-11ce-b03a-0020af0ba770")]  
            public interface IMediaEvent  
            {  
                HRESULT GetEventHandle(out IntPtr hEvent);  
                [PreserveSig]  
                HRESULT GetEvent(out int lEventCode, out IntPtr lParam1, out IntPtr lParam2, int msTimeout);  
                HRESULT WaitForCompletion(int msTimeout, out int pEvCode);  
                HRESULT CancelDefaultHandling(long lEvCode);  
                HRESULT RestoreDefaultHandling(int lEvCode);  
                HRESULT FreeEventParams(int lEvCode, IntPtr lParam1, IntPtr lParam2);  
            }  
      
            [ComImport()]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            [Guid("93E5A4E0-2D50-11d2-ABFA-00A0C9C6E38D")]  
            public interface ICaptureGraphBuilder2  
            {  
                HRESULT SetFiltergraph(IGraphBuilder pfg);  
                HRESULT GetFiltergraph(out IGraphBuilder ppfg);  
                HRESULT SetOutputFileName(ref Guid pType, string lpstrFile, out IBaseFilter ppf, out IFileSinkFilter ppSink);  
                HRESULT FindInterface(ref Guid pCategory, ref Guid pType, IBaseFilter pf, ref Guid riid, out IntPtr ppint);  
                //HRESULT RenderStream(ref Guid pCategory, ref Guid pType, IUnknown pSource,IBaseFilter pfCompressor,IBaseFilter pfRenderer);  
                [PreserveSig]  
                HRESULT RenderStream(ref Guid pCategory, ref Guid pType, IntPtr pSource, IBaseFilter pfCompressor, IBaseFilter pfRenderer);  
                HRESULT ControlStream(ref Guid pCategory, ref Guid pType, IBaseFilter pFilter, Int64 pstart, Int64 pstop, ushort wStartCookie, ushort wStopCookie);  
                HRESULT AllocCapFile(string lpstr, UInt64 dwlSize);  
                HRESULT CopyCaptureFile(string lpwstrOld, string lpwstrNew, int fAllowEscAbort, IAMCopyCaptureFileProgress pCallback);  
                //HRESULT FindPin(IUnknown pSource, PIN_DIRECTION pindir, ref Guid pCategory, ref Guid pType, bool fUnconnected, int num, out IPin ppPin);  
                HRESULT FindPin(IntPtr pSource, PIN_DIRECTION pindir, ref Guid pCategory, ref Guid pType, bool fUnconnected, int num, out IPin ppPin);  
            }  
      
            [ComImport()]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            [Guid("a2104830-7c70-11cf-8bce-00aa00a3f1a6")]  
            public interface IFileSinkFilter  
            {  
                HRESULT SetFileName(string pszFileName, AM_MEDIA_TYPE pmt);  
                HRESULT GetCurFile(out string ppszFileName, out AM_MEDIA_TYPE pmt);  
            }  
      
            [ComImport()]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            [Guid("670d1d20-a068-11d0-b3f0-00aa003761c5")]  
            public interface IAMCopyCaptureFileProgress  
            {  
                HRESULT Progress(int iProgress);  
            }  
      
            [ComImport()]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            [Guid("29840822-5B84-11D0-BD3B-00A0C911CE86")]  
            public interface ICreateDevEnum  
            {  
                HRESULT CreateClassEnumerator(ref Guid clsidDeviceClass, out IEnumMoniker ppEnumMoniker, int dwFlags);  
            }  
      
            [ComImport]  
            [Guid("00000102-0000-0000-C000-000000000046")]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            public interface IEnumMoniker  
            {  
                HRESULT Next(uint celt, out IMoniker rgelt, out uint pceltFetched);  
                HRESULT Skip(uint celt);  
                HRESULT Reset();  
                HRESULT Clone(out IEnumMoniker ppenum);  
            }  
      
            [ComImport]  
            [Guid("0000000f-0000-0000-C000-000000000046")]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            public interface IMoniker : IPersistStream  
            {  
                #region IPersist  
                new HRESULT GetClassID([Out] out Guid pClassID);  
                #endregion  
      
                #region IPersistStream  
                new HRESULT IsDirty();  
                new HRESULT Load(System.Runtime.InteropServices.ComTypes.IStream pStm);  
                new HRESULT Save(System.Runtime.InteropServices.ComTypes.IStream pStm, bool fClearDirty);  
                new HRESULT GetSizeMax(out ULARGE_INTEGER pcbSize);  
                #endregion  
      
                //HRESULT BindToObject(System.Runtime.InteropServices.ComTypes.IBindCtx pbc, IMoniker pmkToLeft, ref Guid riidResult, ref IntPtr ppvResult);  
                HRESULT BindToObject(IntPtr pbc, IMoniker pmkToLeft, ref Guid riidResult, ref IntPtr ppvResult);  
      
                //HRESULT BindToStorage(IBindCtx pbc, IMoniker pmkToLeft, ref Guid riid, out IntPtr ppvObj);  
                HRESULT BindToStorage(IntPtr pbc, IMoniker pmkToLeft, ref Guid riid, out IntPtr ppvObj);  
      
                //HRESULT Reduce(IBindCtx pbc, int dwReduceHowFar, ref IMoniker ppmkToLeft, out IMoniker ppmkReduced);  
                HRESULT Reduce(IntPtr pbc, int dwReduceHowFar, ref IMoniker ppmkToLeft, out IMoniker ppmkReduced);  
      
                HRESULT ComposeWith(IMoniker pmkRight, bool fOnlyIfNotGeneric, out IMoniker ppmkComposite);  
      
                HRESULT Enum(bool fForward, out IEnumMoniker ppenumMoniker);  
      
                HRESULT IsEqual(IMoniker pmkOtherMoniker);  
      
                HRESULT Hash(out int pdwHash);  
      
                //HRESULT IsRunning(IBindCtx pbc, IMoniker pmkToLeft, IMoniker pmkNewlyRunning);  
                HRESULT IsRunning(IntPtr pbc, IMoniker pmkToLeft, IMoniker pmkNewlyRunning);  
      
                //HRESULT GetTimeOfLastChange(IBindCtx pbc, IMoniker pmkToLeft, out FILETIME pFileTime);  
                HRESULT GetTimeOfLastChange(IntPtr pbc, IMoniker pmkToLeft, out System.Runtime.InteropServices.ComTypes.FILETIME pFileTime);  
      
                HRESULT Inverse(out IMoniker ppmk);  
      
                HRESULT CommonPrefixWith(IMoniker pmkOther, out IMoniker ppmkPrefix);  
      
                HRESULT RelativePathTo(IMoniker pmkOther, out IMoniker ppmkRelPath);  
      
                //HRESULT GetDisplayName(IBindCtx pbc, IMoniker pmkToLeft, out LPOLESTR ppszDisplayName);  
                HRESULT GetDisplayName(IntPtr pbc, IMoniker pmkToLeft, out string ppszDisplayName);  
      
                //HRESULT ParseDisplayName(IBindCtx pbc, IMoniker pmkToLeft, LPOLESTR pszDisplayName, out uint pchEaten, out IMoniker ppmkOut);  
                HRESULT ParseDisplayName(IntPtr pbc, IMoniker pmkToLeft, string pszDisplayName, out uint pchEaten, out IMoniker ppmkOut);  
      
                HRESULT IsSystemMoniker(out int pdwMksys);  
            }  
      
            [ComImport]  
            [Guid("00000109-0000-0000-C000-000000000046")]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            public interface IPersistStream : IPersist  
            {  
                #region IPersist  
                new HRESULT GetClassID([Out] out Guid pClassID);  
                #endregion  
                HRESULT IsDirty();  
                HRESULT Load(System.Runtime.InteropServices.ComTypes.IStream pStm);  
                HRESULT Save(System.Runtime.InteropServices.ComTypes.IStream pStm, bool fClearDirty);  
                HRESULT GetSizeMax(out ULARGE_INTEGER pcbSize);  
            }  
      
            [StructLayout(LayoutKind.Explicit)]  
            public struct ULARGE_INTEGER  
            {  
                [FieldOffset(0)]  
                public int LowPart;  
                [FieldOffset(4)]  
                public int HighPart;  
                [FieldOffset(0)]  
                public long QuadPart;  
            }  
      
            [ComImport]  
            [Guid("55272A00-42CB-11CE-8135-00AA004BB851")]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            public interface IPropertyBag  
            {  
                //HRESULT Read(string pszPropName, ref VARIANT* pVar, IErrorLog pErrorLog);  
                //HRESULT Read(string pszPropName, ref IntPtr pVar, IErrorLog pErrorLog);  
                HRESULT Read(string pszPropName, out PROPVARIANT pVar, IErrorLog pErrorLog);  
      
                //HRESULT Write(string pszPropName, VARIANT* pVar);  
                HRESULT Write(string pszPropName, IntPtr pVar);  
            }  
      
            [ComImport]  
            [Guid("3127CA40-446E-11CE-8135-00AA004BB851")]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            public interface IErrorLog  
            {  
                //HRESULT AddError(LPCOLESTR pszPropName, System.Runtime.InteropServices.ComTypes.EXCEPINFO pExcepInfo);  
                HRESULT AddError([In, MarshalAs(UnmanagedType.LPWStr)] string pszPropName, System.Runtime.InteropServices.ComTypes.EXCEPINFO pExcepInfo);  
            }  
      
            [StructLayout(LayoutKind.Sequential)]  
            public struct PROPARRAY  
            {  
                public UInt32 cElems;  
                public IntPtr pElems;  
            }  
      
            [StructLayout(LayoutKind.Explicit, Pack = 1)]  
            public struct PROPVARIANT  
            {  
                [FieldOffset(0)]  
                public ushort varType;  
                [FieldOffset(2)]  
                public ushort wReserved1;  
                [FieldOffset(4)]  
                public ushort wReserved2;  
                [FieldOffset(6)]  
                public ushort wReserved3;  
      
                [FieldOffset(8)]  
                public byte bVal;  
                [FieldOffset(8)]  
                public sbyte cVal;  
                [FieldOffset(8)]  
                public ushort uiVal;  
                [FieldOffset(8)]  
                public short iVal;  
                [FieldOffset(8)]  
                public UInt32 uintVal;  
                [FieldOffset(8)]  
                public Int32 intVal;  
                [FieldOffset(8)]  
                public UInt64 ulVal;  
                [FieldOffset(8)]  
                public Int64 lVal;  
                [FieldOffset(8)]  
                public float fltVal;  
                [FieldOffset(8)]  
                public double dblVal;  
                [FieldOffset(8)]  
                public short boolVal;  
                [FieldOffset(8)]  
                public IntPtr pclsidVal; // GUID ID pointer  
                [FieldOffset(8)]  
                public IntPtr pszVal; // Ansi string pointer  
                [FieldOffset(8)]  
                public IntPtr pwszVal; // Unicode string pointer  
                [FieldOffset(8)]  
                public IntPtr punkVal; // punkVal (interface pointer)  
                [FieldOffset(8)]  
                public PROPARRAY ca;  
                [FieldOffset(8)]  
                public System.Runtime.InteropServices.ComTypes.FILETIME filetime;  
            }  
      
            public enum VARENUM  
            {  
                VT_EMPTY = 0,  
                VT_NULL = 1,  
                VT_I2 = 2,  
                VT_I4 = 3,  
                VT_R4 = 4,  
                VT_R8 = 5,  
                VT_CY = 6,  
                VT_DATE = 7,  
                VT_BSTR = 8,  
                VT_DISPATCH = 9,  
                VT_ERROR = 10,  
                VT_BOOL = 11,  
                VT_VARIANT = 12,  
                VT_UNKNOWN = 13,  
                VT_DECIMAL = 14,  
                VT_I1 = 16,  
                VT_UI1 = 17,  
                VT_UI2 = 18,  
                VT_UI4 = 19,  
                VT_I8 = 20,  
                VT_UI8 = 21,  
                VT_INT = 22,  
                VT_UINT = 23,  
                VT_VOID = 24,  
                VT_HRESULT = 25,  
                VT_PTR = 26,  
                VT_SAFEARRAY = 27,  
                VT_CARRAY = 28,  
                VT_USERDEFINED = 29,  
                VT_LPSTR = 30,  
                VT_LPWSTR = 31,  
                VT_RECORD = 36,  
                VT_INT_PTR = 37,  
                VT_UINT_PTR = 38,  
                VT_FILETIME = 64,  
                VT_BLOB = 65,  
                VT_STREAM = 66,  
                VT_STORAGE = 67,  
                VT_STREAMED_OBJECT = 68,  
                VT_STORED_OBJECT = 69,  
                VT_BLOB_OBJECT = 70,  
                VT_CF = 71,  
                VT_CLSID = 72,  
                VT_VERSIONED_STREAM = 73,  
                VT_BSTR_BLOB = 0xfff,  
                VT_VECTOR = 0x1000,  
                VT_ARRAY = 0x2000,  
                VT_BYREF = 0x4000,  
                VT_RESERVED = 0x8000,  
                VT_ILLEGAL = 0xffff,  
                VT_ILLEGALMASKED = 0xfff,  
                VT_TYPEMASK = 0xfff  
            };  
      
      
            [ComImport]  
            [Guid("5a804648-4f66-4867-9c43-4f5c822cf1b8")]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            public interface IVMRFilterConfig9  
            {  
                HRESULT SetImageCompositor(IVMRImageCompositor9 lpVMRImgCompositor);  
                HRESULT SetNumberOfStreams(uint dwMaxStreams);  
                HRESULT GetNumberOfStreams(out uint pdwMaxStreams);  
                HRESULT SetRenderingPrefs(uint dwRenderFlags);  
                HRESULT GetRenderingPrefs(out uint pdwRenderFlags);  
                HRESULT SetRenderingMode(uint Mode);     
                HRESULT GetRenderingMode(out uint pMode);  
            }  
      
            [ComImport]  
            [Guid("4a5c89eb-df51-4654-ac2a-e48e02bbabf6")]  
            [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
            public interface IVMRImageCompositor9  
            {  
                HRESULT InitCompositionDevice(IntPtr pD3DDevice);  
                HRESULT TermCompositionDevice(IntPtr pD3DDevice);  
                HRESULT SetStreamMediaType(uint dwStrmID, AM_MEDIA_TYPE pmt, bool fTexture);  
                //HRESULT CompositeImage(IntPtr pD3DDevice, IDirect3DSurface9* pddsRenderTarget, AM_MEDIA_TYPE pmtRenderTarget, long rtStart,  
                //     long rtEnd, uint dwClrBkGnd, VMR9VideoStreamInfo pVideoStreamInfo, uint cStreams);  
                HRESULT CompositeImage(IntPtr pD3DDevice, IntPtr pddsRenderTarget, AM_MEDIA_TYPE pmtRenderTarget, long rtStart,  
                    long rtEnd, uint dwClrBkGnd, ref VMR9VideoStreamInfo pVideoStreamInfo, uint cStreams);  
            }  
      
            [StructLayout(LayoutKind.Sequential)]  
            public struct VMR9VideoStreamInfo  
            {  
                //public IDirect3DSurface9* pddsVideoSurface;  
                public IntPtr pddsVideoSurface;  
                public uint dwWidth;  
                public uint dwHeight;  
                public uint dwStrmID;  
                public float fAlpha;  
                VMR9NormalizedRect rNormal;  
                long rtStart;  
                long rtEnd;  
                VMR9_SampleFormat SampleFormat;
    

  5. Amin Dodin 1 Reputation point
    2021-03-29T05:50:17.067+00:00

    Bradley,

    You could use the Capture Control from LEADTOOLS multimedia SDK. (Disclosure: I’m a LEAD employee).

    The following code shows how to do the following:

    1. Create a Capture control by code. This can also be done by placing the control on a form at design time.
    2. List all the video sources (e.g. cameras) and retrieve their names. This can be used to fill a list box or build a menu for the user to enable selecting the desired device.
    3. Select one of the devices using its index.
    4. Capture a still bitmap when the user presses a key. This can be done in a Winform button or menu item while the Capture control displays a preview of the video.
    5. Save the captured image to a disk file. It is also possible to take this image and process it either using LEADTOOLS image processing functions or by any other means. CaptureCtrl cap = new CaptureCtrl(true);
      // list all available cameras and video sources
      for (int i = 0; i < cap.VideoDevices.Count; i++)
      Console.WriteLine($"Device No. {i}: {cap.VideoDevices[i].FriendlyName}"); // can be used to fill a list box or combo box
      cap.VideoDevices.Selection = desiredDeviceNumber;
      cap.ReadyCapture(CaptureMode.Still);
      Console.WriteLine("Pausing.. Press any key to capture from camera");
      Console.ReadKey(true);
      cap.StartCapture(CaptureMode.Still);
      System.Drawing.Image image = cap.GetStillImage(-1);
      image.Save(@"G:\Test\CameraSnapshot.png", System.Drawing.Imaging.ImageFormat.Png);

    As you can see, the code is almost shorter than its description!

    If you would like to try it, there’s a free evaluation edition on this page. There is also free technical support through email and chat during evaluation (and after purchasing), in case you need help using the LEADTOOLS controls and classes.

    0 comments No comments