简体   繁体   English

使用Microsoft Expression Encoder SDK捕获静止图像

[英]Capture still image with Microsoft Expression Encoder SDK

I was playing around with my webcam and started to create a small application (using the Microsoft Expression Encoder SDK), where the images of the webcam are streamed to a picturebox on a winform [1]. 我在玩网络摄像头,并开始创建一个小应用程序(使用Microsoft Expression Encoder SDK),在其中将网络摄像头的图像流式传输到winform上的图片框[1]。 So far, everything went pretty smooth. 到目前为止,一切都很顺利。 But now my problem begins: 但是现在我的问题开始了:

I want to capture a single image of the video stream and store it. 我想捕获视频流的单个图像并将其存储。 I've found the "ScreenCaptureJob" class, which is able to create video files. 我找到了“ ScreenCaptureJob”类,该类能够创建视频文件。 Microsoft's MSDN states that it is possible to "capture anything from a still image of a dialog box" [2] to complete videos. 微软的MSDN指出,可以“从对话框的静止图像中捕获任何东西” [2]来完成视频。 All the samples in the MSDN refer to video capturing. MSDN中的所有示例均涉及视频捕获。 Unfortunately I couldn't find any solution how to use this class to capture a single image. 不幸的是,我找不到如何使用此类捕获单个图像的解决方案。

Can anyone help me? 谁能帮我?

[1] Code to stream webcam to picture box (Source: http://www.codeproject.com/Articles/202464/How-to-use-a-WebCam-in-C-with-the-NET-Framework-4 ) [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 [2] http://msdn.microsoft.com/zh-CN/library/gg602440%28v=expression.40%29.aspx

You can use the library for a still capture but it appears to be a bit of a kludge. 您可以使用该库进行静态捕获,但似乎有点麻烦。 (I am still looking for a better solution) I found an example at link The basic solution is to pop up a preview window and then using a graphics object of the same dimensions, use CopyFromScreen() to get the file. (我仍在寻找更好的解决方案)我在链接上找到了一个示例。基本解决方案是弹出预览窗口,然后使用相同尺寸的图形对象,使用CopyFromScreen()获取文件。

You can but it appears to be a bit of a kludge. 您可以,但似乎有点不合时宜。 I found an example at Code Project-How To Use A Webcam in C# The basic solution is to pop up a preview window. 我在“代码项目-如何在C#中使用网络摄像头”中找到了一个示例,基本解决方案是弹出预览窗口。 Then, using a graphics object of the same dimensions, use CopyFromScreen() to get the file. 然后,使用相同尺寸的图形对象,使用CopyFromScreen()获取文件。 Here is the code: 这是代码:

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(....)
    }

I'm not sure if it's possible with Microsoft Expression Encoder SDK, it seems to be poorly documented. 我不确定是否可以使用Microsoft Expression Encoder SDK,它的文档记录似乎很少。

But you can use Video Capture functions instead. 但是您可以改用视频捕获功能。

Just create a preview window using capCreateCaptureWindow function and then register frame callback using by sending WM_CAP_SET_CALLBACK_FRAME message: 只需使用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);

You can find C# examples here and here . 您可以在此处此处找到C#示例。

And if you figure out how to do that with Expression Encoder, please let me know. 如果您知道如何使用Expression Encoder做到这一点,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM