Photo and video with webcam using DirectShowLib-2005

送分小仙女□ 提交于 2019-12-14 02:23:51

问题


I'm trying to take pictures with a webcam in my c# application. I found out to use DirectShowLib. After a hard research, I found an example of how using the webcam, it displays the video on screen perfect, but it can't take the bitmap image for saving it at my will.

sample of the code I found to take the picure would be

    public Bitmap snapImage()
    {
        IVMRWindowlessControl9 windowlessCtrl = null;
        IBaseFilter vmr9 = null;
        vmr9 = (IBaseFilter)new DirectShowLib.VideoMixingRenderer9();
        DirectShowLib.IVMRFilterConfig9 filterConfig = (DirectShowLib.IVMRFilterConfig9)vmr9;
        int hr = filterConfig.SetNumberOfStreams(1);
        hr = filterConfig.SetRenderingMode(DirectShowLib.VMR9Mode.Windowless);

        windowlessCtrl = (IVMRWindowlessControl9)vmr9;
        hr = windowlessCtrl.SetVideoClippingWindow(this.PreviewWindow.Handle);
        hr = windowlessCtrl.SetAspectRatioMode(VMR9AspectRatioMode.LetterBox);

        IntPtr currentImage = IntPtr.Zero;

        Bitmap bmp = null;

        //this is the line in wich I have problems
        hr = windowlessCtrl.GetCurrentImage(out currentImage);

        BitmapInfoHeader structure = new BitmapInfoHeader();
        Marshal.PtrToStructure(currentImage, structure);

        bmp = new Bitmap(structure.Width, structure.Height, (structure.BitCount / 8) * structure.Width, System.Drawing.Imaging.PixelFormat.Format32bppArgb, new IntPtr(currentImage.ToInt64() + 40));

        return bmp;
    }

And I got: "Error HRESULT E_FAIL has been returned from a call to a COM component" with error code -2147467259

Can sample I found is in this page http://www.codeproject.com/Articles/34663/DirectShow-Examples-for-Using-SampleGrabber-for-Gr

Can anyone help me to take the snapshot? or can any one tell me how to do what I want to do (show video and take snapshot of webcam)

Thankk you


回答1:


GetCurrentImage might return E_FAIL (behavior by design) until a real video frame is displayed, which takes place asynchronously. To check this out simply display a message box before the call in question, wait until video starts streaming, close the box and you should have the call working as you originally expected.



来源:https://stackoverflow.com/questions/28668693/photo-and-video-with-webcam-using-directshowlib-2005

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!