C#代码调用摄像头

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;
        }
    }
}

拍照效果:


重拍效果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值