C# winform RealSense视频显示

 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”项目

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要在C# Winform显示当前时间,你可以使用Timer控件和Label控件。首先,在窗体加载时,你可以使用以下代码将当前日期和星期转换为中文,并将其保存在一个变量中\[2\]: ```csharp string weekstr = ""; private void Form_Load(object sender, EventArgs e) { // 将定时器启动 timer1.Start(); // 将得到的星期转换成中文 switch (DateTime.Now.DayOfWeek.ToString()) { case "Monday": weekstr = "星期一"; break; case "Tuesday": weekstr = "星期二"; break; case "Wednesday": weekstr = "星期三"; break; case "Thursday": weekstr = "星期四"; break; case "Friday": weekstr = "星期五"; break; case "Saturday": weekstr = "星期六"; break; case "Sunday": weekstr = "星期日"; break; } } ``` 然后,你可以使用Timer控件的Tick事件来更新Label控件的文本,以显示当前时间和星期\[2\]: ```csharp private void timer1_Tick(object sender, EventArgs e) { label1.Text = "当前时间:" + DateTime.Now.ToString() + " " + weekstr; } ``` 确保在窗体中拖放一个Timer控件,并将其Enable属性设置为true,Interval属性设置为1000(1秒)\[2\]。这样,每秒钟Timer控件的Tick事件就会触发一次,更新Label控件的文本,从而显示当前时间和星期。 #### 引用[.reference_title] - *1* [C#winform【在状态栏显示实时时间】--实战练习一](https://blog.csdn.net/BoomBiuBiu/article/details/122616095)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [C#Winform显示当前日期、时间、星期](https://blog.csdn.net/whb78952/article/details/124712447)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值