1.下载WPFMediaKit包
文章图片
2.XAML界面
3.后端逻辑
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
namespace 拍照截图
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
string[] inputNames = MultimediaUtil.VideoInputNames;
m_VideoCaptureElement.VideoCaptureSource = inputNames[0];
}private void button1_Click(object sender, RoutedEventArgs e)
{
//调用默认摄像头
RenderTargetBitmap bmp = new RenderTargetBitmap((int)m_VideoCaptureElement.ActualWidth, (int)m_VideoCaptureElement.ActualHeight, 96, 96, PixelFormats.Default);
bmp.Render(m_VideoCaptureElement);
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;
//保存路径D盘根目录
string filename = "D:\\" + now + ".jpg";
FileStream fstream = new FileStream(filename, FileMode.Create);
encoder.Save(fstream);
fstream.Close();
MessageBox.Show("图片已经保存到D盘根目录");
}
}
}
4.效果展示,下载地址及注意
文章图片
文章图片
https://pan.baidu.com/s/1Czul8NQX0qavhyYZWdCiMA
提取码
86n3
【C#与WPF|WPF实现拍照截图功能(WPFMediaKit 调用摄像头和拍照)】注意:
1.界面引用组件需要加一句
xmlns:WPFMediaKit="clr-namespace:WPFMediaKit.DirectShow.Controls;
assembly=WPFMediaKit"
2.截图保存到D盘根目录,路径可以自己设置.
推荐阅读
- C#|C# 文件路径操作
- WPF圆形进度条制作
- WPF|导航通用菜单按钮样式
- wpf——自定义进度条控件
- C#|Visual Studio2019生成安装包
- dotnet|通过 AppSwitch 禁用 WPF 内置的触摸让 WPF 程序可以处理 Windows 触摸消息
- WPF的自定义播放进度条样式