private void CloseGraph()
        {
            FilterState state;

              if (mediaControl != null)
              {
            do
            {
              mediaControl.Stop();
              mediaControl.GetState(0, out state);
            } while (state != FilterState.Stopped);

            mediaControl = null;
              }

              if (allocator != null)
              {
            allocator.Dispose();
            allocator = null;
              }

              if (filter != null)
              {
            Marshal.ReleaseComObject(filter);
            filter = null;
              }

              if (graph != null)
              {
            RemoveAllFilters();

            Marshal.ReleaseComObject(graph);
            graph = null;
              }
        }
        private void SetAllocatorPresenter()
        {
            int hr = 0;

              IVMRSurfaceAllocatorNotify9 vmrSurfAllocNotify = (IVMRSurfaceAllocatorNotify9) filter;

              try
              {
            allocator = new Allocator(this);

            hr = vmrSurfAllocNotify.AdviseSurfaceAllocator(userId, allocator);
            DsError.ThrowExceptionForHR(hr);

            hr = allocator.AdviseNotify(vmrSurfAllocNotify);
            DsError.ThrowExceptionForHR(hr);
              }
              catch
              {
            allocator = null;
            throw;
              }
        }
        public void InitVMR9(string filename)
        {
            int hr = 0;

              // Create a DirectShow FilterGraph
              graphBuilder = (IFilterGraph2) new FilterGraph();

              // Add it in ROT for debug purpose
              rot = new DsROTEntry(graphBuilder);

              // Add a notification handler (see WndProc)
              hr = (graphBuilder as IMediaEventEx).SetNotifyWindow(this.Handle, WM_GRAPHNOTIFY, IntPtr.Zero);
              DsError.ThrowExceptionForHR(hr);

              // Create a VMR9 object
              vmr9 = (IBaseFilter) new VideoMixingRenderer9();

              IVMRFilterConfig9 filterConfig = (IVMRFilterConfig9) vmr9;

              // We want the Renderless mode!
              hr = filterConfig.SetRenderingMode(VMR9Mode.Renderless);
              DsError.ThrowExceptionForHR(hr);

              // Create the Allocator / Presenter object
              allocator = new Allocator(device);

              IVMRSurfaceAllocatorNotify9 vmrSurfAllocNotify = (IVMRSurfaceAllocatorNotify9) vmr9;

              // Notify the VMR9 filter about our allocator
              hr = vmrSurfAllocNotify.AdviseSurfaceAllocator(IntPtr.Zero, allocator);
              DsError.ThrowExceptionForHR(hr);

              // Notify our allocator about the VMR9 filter
              hr = allocator.AdviseNotify(vmrSurfAllocNotify);
              DsError.ThrowExceptionForHR(hr);

              if (config.IsUsingMixing)
              {
            // One stream is enough for this sample
            // This line also load the mixing componant
            hr = filterConfig.SetNumberOfStreams(1);
            DsError.ThrowExceptionForHR(hr);

            IVMRMixerControl9 mixerControl = (IVMRMixerControl9) vmr9;

            // Select the mixer mode : YUV or RGB
            if (config.UseYUVMixing)
            {
              hr = mixerControl.SetMixingPrefs(VMR9MixerPrefs.RenderTargetYUV | VMR9MixerPrefs.NoDecimation | VMR9MixerPrefs.ARAdjustXorY | VMR9MixerPrefs.BiLinearFiltering);
            }
            else
            {
              hr = mixerControl.SetMixingPrefs(VMR9MixerPrefs.RenderTargetRGB | VMR9MixerPrefs.NoDecimation | VMR9MixerPrefs.ARAdjustXorY | VMR9MixerPrefs.BiLinearFiltering);
            }
            DsError.ThrowExceptionForHR(hr);

            Debug.WriteLine("Using VMR9 mixing mode");
              }

              // Add the filter to the graph
              hr = graphBuilder.AddFilter(vmr9, "Video Mixing Renderer 9");
              DsError.ThrowExceptionForHR(hr);

              // Render the file
              hr = graphBuilder.RenderFile(filename, null);
              DsError.ThrowExceptionForHR(hr);

              // Run the graph
              hr = (graphBuilder as IMediaControl).Run();
              DsError.ThrowExceptionForHR(hr);
        }