using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPFMediaKit.DirectShow.Controls;
using System.IO;
namespace Camera
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
cbCarema.ItemsSource = MultimediaUtil.VideoInputNames;
if (MultimediaUtil.VideoInputNames.Length > 0)
{
cbCarema.SelectedIndex = 0;
}
else
{
MessageBox.Show("本电脑没有安装摄像设备");
}
}
private void btnPhoto_Click(object sender, RoutedEventArgs e)
{
//captureElement. 怎么抓取高清的原始图像
//能不能抓视频。
//todo:怎么只抓取一部分
if(btnPhoto.Content.ToString()=="拍照")
{
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(@"F:\photo.jpg", CaptureData);
}
MessageBox.Show(@"拍照成功,位置:F:\photo.jpg");
}
//captureElement.Pause();
//todo:自己完成重拍的代码
//DialogResult = true;
if(btnPhoto.Content.ToString()=="重拍")
{
RenderTargetBitmap bmp1 = new RenderTargetBitmap(
(int)vce.ActualWidth, (int)vce.ActualHeight,
96, 96, PixelFormats.Default);
bmp1.Render(vce);
BitmapEncoder encoder1 = new JpegBitmapEncoder();
encoder1.Frames.Add(BitmapFrame.Create(bmp1));
using (MemoryStream ms1 = new MemoryStream())
{
encoder1.Save(ms1);
byte[] CaptureData1 = ms1.ToArray();
File.WriteAllBytes(@"F:\RePhoto.jpg", CaptureData1);
}
MessageBox.Show(@"重拍成功,位置:F:\RePhoto.jpg");
}
btnPhoto.Content = "重拍";
}
private void cbCarema_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
vce.VideoCaptureSource = (string)cbCarema.SelectedItem;
}
}
}
拍照效果:
重拍效果: