Intel.RealSense摄像头显示 winform

最终效果:

 1.下载安装SDK,得到Intel.RealSense.dll    https://github.com/IntelRealSense/librealsense/releases      Intel® RealSense™ SDK 2.0 (v2.49.0)

using Intel.RealSense;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        Intel.RealSense.Context cxt = new Intel.RealSense.Context();
        Device device = null;   // 选择的Intel Realsense相机
        private Pipeline pipeline = new Pipeline();

        private CancellationTokenSource tokenSource_gua = new CancellationTokenSource();

        private delegate void SetRealSenceVedio(Bitmap bitmap);
        public Form1()
        {
            InitializeComponent();
            //tokenSource_gua = new CancellationTokenSource();
            CheckForIllegalCrossThreadCalls = false;
        }

        private void open_Click(object sender, EventArgs e)
        {
            cmbbox_camDev.Text = string.Empty;
            cmbbox_camDev.Items.Clear();

            foreach (var device in cxt.Devices)
            {
                cmbbox_camDev.Items.Add(device.Info.GetInfo(CameraInfo.SerialNumber));
            }           
        }

        private void pBox_vedio_Click(object sender, EventArgs e)
        {

        }

        private void btn_try_Click(object sender, EventArgs e)
        {
            tokenSource_gua = new CancellationTokenSource();
            foreach (var dev in cxt.Devices)
            {
                if (dev.Info.GetInfo(CameraInfo.SerialNumber) == cmbbox_camDev.Text)
                {
                    device = dev;
                }
            }

            if (device == null)
            {
                MessageBox.Show("找不到对应的Realsense相机!");
                return;
            }

            var cfg = new Config();
            cfg.EnableStream(Intel.RealSense.Stream.Color, 1280, 720, Format.Bgr8, 30);

            var selection = pipeline.Start(cfg);

            System.Threading.Tasks.Task.Factory.StartNew(() =>
            {
                while (!tokenSource_gua.Token.IsCancellationRequested)
                {
                    #region 显示帧的时候必须使用using,否则提示Frame didn't arrived within 5000,Frame没得到.net正确释放造成的。//
                    using (var frames = pipeline.WaitForFrames())
                    {
                        VideoFrame frame = frames.ColorFrame.DisposeWith(frames);
                        //Format24bppRgb 色差(指定格式为每像素 24 位;红色、绿色和蓝色分量各使用 8 位。跟 cfg.EnableStream(Format.Rgb8)相关)
                        Bitmap newBitmap = new Bitmap(frame.Width, frame.Height, frame.Stride, PixelFormat.Format24bppRgb, frame.Data);
                        Bitmap bitmap2 = new Bitmap(newBitmap);
                        newBitmap.Dispose();
                        Console.WriteLine("s: {0}", frame.Number);

                        Dispatcher.CurrentDispatcher.Invoke(new SetRealSenceVedio(SetRealSenceValue), bitmap2);
                    }
                    #endregion                        
                }
                pipeline.Stop();
            }, tokenSource_gua.Token);
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            tokenSource_gua?.Cancel();
        }


        private void SetRealSenceValue(Bitmap bitmap)
        {
            this.pBox_vedio.Image = bitmap;
        }
    }
}

  

原文地址:https://www.cnblogs.com/zhangkun35268/p/15250977.html