1.下载WPFMediaKit包
2.XAML界面
<Window x:Class="拍照截图.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:拍照截图"
mc:Ignorable="d"
xmlns:WPFMediaKit="clr-namespace:WPFMediaKit.DirectShow.Controls;assembly=WPFMediaKit"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Button Name="button1" Content="截图" Height="23" Width="60" Click="button1_Click" />
</StackPanel>
<WPFMediaKit:VideoCaptureElement x:Name="m_VideoCaptureElement" Grid.Row="1" ></WPFMediaKit:VideoCaptureElement>
</Grid>
</Window>
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 拍照截图
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
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
注意:
1.界面引用组件需要加一句
xmlns:WPFMediaKit="clr-namespace:WPFMediaKit.DirectShow.Controls;assembly=WPFMediaKit"
2.截图保存到D盘根目录,路径可以自己设置.