版权声明:博主原创/资料整理,转载请注明出处!! https://blog.csdn.net/tiegenZ/article/details/80509292
需要用到的dll: WPFMediaKit.dll
创建一个WPF应用
首先引入WPFMediaKit.dll ,然后在xaml文件中如下使用wpfmedia:VideoCaptureElement 控件,注意需要先引用
xmlns:wpfmedia="clr-namespace:WPFMediaKit.DirectShow.Controls;assembly=WPFMediaKit"
按钮点击事件代码如下(将图片保存到D盘 以当前时间命名):
private void btnCapture_Click(object sender, RoutedEventArgs e)
{
DateTime dt =DateTime.Now;
//
RenderTargetBitmap bmp = new RenderTargetBitmap((int)vce.ActualWidth, (int)vce.ActualHeight,96, 96, PixelFormats.Default);
bmp.Render(vce);
BitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
using (MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
byte[] captureData = ms.ToArray();
//保存图片
File.WriteAllBytes("d:/"+
dt.Year.ToString()+
dt.Month.ToString() +
dt.Day.ToString() +
dt.Hour.ToString() +
dt.Minute.ToString() +
dt.Second.ToString() +
".jpg", captureData);
}
vce.Pause();
MessageBox.Show("照片已保存");
vce.VideoCaptureSource="";
}
private void btn_Click(object sender, RoutedEventArgs e)
{
string[] ss = MultimediaUtil.VideoInputNames;
foreach (string s in ss)
{
vce.VideoCaptureSource = s;
}
}
效果图: