2013-04-25 46 views
-1

我正在玩弄网络摄像头,并开始创建一个小型应用程序(使用Microsoft Expression Encoder SDK),其中网络摄像头的图像将流式传输到winform上的图片框[1 ]。到目前为止,一切都非常顺利。但现在我的问题开始:使用Microsoft Expression Encoder SDK捕获静止图像

我想捕获视频流的单个图像并将其存储。我找到了能够创建视频文件的“ScreenCaptureJob”类。微软的MSDN声称可以“从对话框的静止图像中捕捉任何东西”[2]来完成视频。 MSDN中的所有示例均指视频捕获。不幸的是,我找不到任何解决方案如何使用这个类来捕捉单个图像。

任何人都可以帮助我吗?

[1]代码以流的摄像头,图片框(来源:http://www.codeproject.com/Articles/202464/How-to-use-a-WebCam-in-C-with-the-NET-Framework-4

 var lstVideoDevices = new Dictionary<string, EncoderDevice>(); 
     var lstAudioDevices = new Dictionary<string, EncoderDevice>(); 

     foreach (EncoderDevice edv in EncoderDevices.FindDevices(EncoderDeviceType.Video)) 
     { 
      lstVideoDevices.Add(edv.Name, edv); 
     } 
     foreach (EncoderDevice eda in EncoderDevices.FindDevices(EncoderDeviceType.Audio)) 
     { 
      lstAudioDevices.Add(eda.Name, eda); 
     } 

     _job = new 

     var _deviceSource = _job.AddDeviceSource(lstVideoDevices.Values.FirstOrDefault(x => x.Name.Contains("USB")), lstAudioDevices.Values.FirstOrDefault(x => x.Name.Contains("USB"))); 

     _deviceSource.PreviewWindow = new PreviewWindow(new HandleRef(this.pictureBox1, this.pictureBox1.Handle)); 

     _job.ActivateSource(_deviceSource);` 

[2] http://msdn.microsoft.com/en-us/library/gg602440%28v=expression.40%29.aspx

回答

0

我不知道是否有可能与微软Expression Encoder的SDK,它似乎记录不当。

但是,您可以使用Video Capture函数。

只需创建一个使用capCreateCaptureWindow功能的预览窗口,然后使用通过发送WM_CAP_SET_CALLBACK_FRAME消息登录框回调:

/* imports */ 
[DllImport("user32", EntryPoint="SendMessage")] 
public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam); 

[DllImport("avicap32.dll", EntryPoint="capCreateCaptureWindowA")] 
public static extern int capCreateCaptureWindowA(string lpszWindowName, int dwStyle, int X, int Y, int nWidth, int nHeight, int hwndParent, int nID); 

/* ... */ 
capCreateCaptureWindowA(lpszName, showVideo.WS_VISIBLE | showVideo.WS_CHILD, 0, 0, mWidth, mHeight, mControlPtr, 0); 

SendMessage(lwnd, showVideo.WM_CAP_SET_CALLBACK_FRAME, 0, handler); 

你可以找到C#示例herehere

如果你想知道如何用Expression Encoder做到这一点,请让我知道。

2

您可以使用该库进行静态捕捉,但看起来有点混乱。 (我仍然在寻找更好的解决方案)我在link找到了一个例子基本的解决方案是弹出预览窗口,然后使用相同尺寸的图形对象,使用CopyFromScreen()获取文件。

你可以但它似乎有点kludge。我在Code Project-How To Use A Webcam in C#处找到了一个例子。基本的解决方案是弹出一个预览窗口。然后,使用相同尺寸的图形对象,使用CopyFromScreen()获取文件。这里是代码:

using (Bitmap bitmap = new Bitmap(panelVideoPreview.Width, panelVideoPreview.Height)) 
    { 
    using (Graphics g = Graphics.FromImage(bitmap)) 
    { 
     // Get the paramters to call g.CopyFromScreen and get the image 
     Rectangle rectanglePanelVideoPreview = panelVideoPreview.Bounds; 
     Point sourcePoints = panelVideoPreview.PointToScreen(new Point(panelVideoPreview.ClientRectangle.X, panelVideoPreview.ClientRectangle.Y)); 
     g.CopyFromScreen(sourcePoints, Point.Empty, rectanglePanelVideoPreview.Size); 
    } 
    bitmap.Save(....) 
    } 
相关问题