WPF实现拍照截图功能(WPFMediaKit 调用摄像头和拍照)

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盘根目录,路径可以自己设置.

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值