尝试使用VideoCaptureElement捕获图像时获取空白图像

时间:2016-04-01 14:41:52

标签: wpf

我正在尝试使用集成网络摄像头捕获图像。我总是保存一张空白图像。下面是我的XMAL和代码。

XMAL

                <WPFMediaKit:VideoCaptureElement   LoadedBehavior="Play"  StretchDirection="Both" FPS="30"
                                   Stretch="Uniform"
                                    VideoCaptureSource="{Binding Path=CaptureDeviceName}"
                                   UnloadedBehavior="Stop"  

                                   EnableSampleGrabbing="true"
            DesiredPixelWidth="1280" DesiredPixelHeight="1024"
            Width="300" Height="300"  x:Name="videoCapElement" 
            HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >

                </WPFMediaKit:VideoCaptureElement>

dev1 = MultimediaUtil.VideoInputDevices.FirstOrDefault();

                try
                {

                    if (dev1 != null)
                    {

                        videoCapElement.VideoCaptureDevice = dev1;
                        videoCapElement.VideoCaptureSource = dev1.Name;
                        videoCapElement.Play();

                        RenderTargetBitmap bmp = new RenderTargetBitmap((int)videoCapElement.Width, (int)videoCapElement.Height, 96d, 96d, PixelFormats.Default);
                        bmp.Render(videoCapElement);
                        BitmapEncoder encoder = new JpegBitmapEncoder();
                        encoder.Frames.Add(BitmapFrame.Create(bmp));
                        string now = DateTime.Now.Year + "" + DateTime.Now.Month + "" + DateTime.Now.Day + "" + DateTime.Now.Hour + "" + DateTime.Now.Minute + "" + DateTime.Now.Second;
                        string filename = now + "pic.jpg";
                        FileStream fstream = new FileStream(filename, FileMode.CreateNew);

                        encoder.Save(fstream);
                        fstream.Close();
                    }

                }

0 个答案:

没有答案