WPF webcam capture using expression encode

1.8k views Asked by At

I’m making an application that will run on a tablet device that has two webcams built in. One of the requirements is to be able to capture images and save them.

So far I have been able to get the preview of the webcam’s output using this code

Dim Job As New LiveJob
Dim source As LiveDeviceSource
source = Job.AddDeviceSource(EncoderDevices.FindDevices(EncoderDeviceType.Video).Item(0), Nothing)

source.PreviewWindow = New PreviewWindow(New HandleRef(Me.panPreview, Me.panPreview.Handle))

Job.ActivateSource(source)

This displays the preview in a hosted winforms panel. The question is how do I capture an image from this stream and return a new image object for later processing?

I have tried capturing the winforms host using RenderTargetBitmap but just returns a black rectangle and it wont let me render the winforms panel.

2

There are 2 answers

0
Srikanth S On

Just found this piece of gem on code project. Here goes the code. Here panelVideoPreview is your preview i.e. panPreview window. hope this helps.

private void cmdGrabImage_Click(object sender, EventArgs e)        
{
// Create a Bitmap of the same dimension of panelVideoPreview (Width x Height)
    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); 
        }
        string strGrabFileName = String.Format("C:\\Snapshot_{0:yyyyMMdd_hhmmss}.jpg", DateTime.Now);
        toolStripStatusLabel1.Text = strGrabFileName;
        bitmap.Save(strGrabFileName, System.Drawing.Imaging.ImageFormat.Jpeg);                
    }
}
0
Eldoran On

If you have a window over the window you want to capture the capture will be an image of the over window or if you minimize the window occurs the same, you'll capture a screenshot of the coordenates. This method is a capture of screen with coordenates.

How it would be the capture image of the streaming?