-1

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]. 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. Microsoft's MSDN states that it is possible to "capture anything from a still image of a dialog box" [2] to complete videos. All the samples in the MSDN refer to video capturing. 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)

        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 Answers 2

2

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.

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. Then, using a graphics object of the same dimensions, use CopyFromScreen() to get the file. 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(....)
    }
1

I'm not sure if it's possible with Microsoft Expression Encoder SDK, it seems to be poorly documented.

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:

/* 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.

And if you figure out how to do that with Expression Encoder, please let me know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.