C# winform RealSense视频显示

该博客介绍了如何利用Intel RealSense SDK 2.0在C# Windows Forms应用中实现Intel Realsense相机的视频流显示。通过下载安装SDK,创建Context和Pipeline对象,配置并启动流,以及处理和显示帧数据,实现了相机的实时视频预览功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

Form1.cs

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://software.intel.com/content/www/us/en/develop/articles/using-the-intel-realsense-sdk-to-create-hello-world-project.html    使用英特尔® 实感™ SDK 在 C#/WPF 中创建“Hello World”项目

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值