如何在C#中实现ISampleGrabberCB类

时间:2012-06-16 21:14:03

标签: c# .net directx directshow

如何在C#中实现ISampleGrabberCB类;我在C#中使用DirectShowLib? 我有这个Overlay类来实现ISampleGrabberCB。

    internal sealed class Overlay : ISampleGrabberCB
    {
    private Bitmap _Overlay;

    private int _Width;
    private int _Height;
    private int _Stride;

    public Size Size { get { return new Size(_Width, _Height); } }

    public Overlay()
    {
    }
    public void Configure(ISampleGrabber Grabber)
    {
        // Get the media type from the SampleGrabber
        AMMediaType Media = new AMMediaType();

       DsError.ThrowExceptionForHR(Grabber.GetConnectedMediaType(Media));

        if (Media.formatType != FormatType.VideoInfo || Media.formatPtr == IntPtr.Zero)
        {
            throw new NotSupportedException("Unknown Grabber Media Format");
        }

        // Grab the size info
        VideoInfoHeader Header = (VideoInfoHeader)Marshal.PtrToStructure
        (Media.formatPtr, typeof(VideoInfoHeader));
        _Width = Header.BmiHeader.Width;
        _Height = Header.BmiHeader.Height;
        _Stride = _Width * (Header.BmiHeader.BitCount / 8);

        DsUtils.FreeAMMediaType(Media);
    }
         }

然后在这个类中我试图使用aoverlay类来回调函数

    public sealed class Grab : IDisposable, IDXLibVideoRenderer
    {
    private Overlay _Overlay;
    ISampleGrabber Grabber = (ISampleGrabber)new SampleGrabber();

                // Set the media type to Video/RBG32
                AMMediaType Media = new AMMediaType();
                Media.majorType = MediaType.Video;
                Media.subType = MediaSubType.RGB32;
                Media.formatType = FormatType.VideoInfo;

                DsError.ThrowExceptionForHR(Grabber.SetMediaType(Media));
                DsUtils.FreeAMMediaType(Media);

               DsError.ThrowExceptionForHR(_FilterGraph.AddFilter 

              ((IBaseFilter)Grabber, "Sample Grabber"));

           _Overlay = new Overlay();
                _Overlay.Configure(Grabber);
                DsError.ThrowExceptionForHR(Grabber.SetCallback(_Overlay, 1));
        }

但我在Overlay.Configure()方法中遇到异常。我上面哪里做错了?

0 个答案:

没有答案