问题
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