目的:输出两台摄像头图像和两路设备图像,每一路设备截图6张

主要知识:

1. 通过SDK调取摄像头图像,并对图像进行剪裁;

2. WPF中定时器DispatcherTimer用法;

3. WPF中跨线程访问控件方法

  Dispatcher.Invoke((Action)delegate {});

区别于winform中

this.Invoke((Action)delegate {});

4.xml操作:

 XmlDocument xmlDoc = new XmlDocument();xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "\\config.xml");XmlNode settingNode = xmlDoc.DocumentElement;XmlElement e = settingNode.SelectSingleNode("DeviceType") as XmlElement;if (e == null){deviceType = "tps2000";}else{deviceType = e.InnerText;}

5. 带多个参数的委托

            DP1 = new DataProcess(DeviceIP1, LocalPort1,0);DP1.ShowEvent1 = DrawControls1;DP1.Start();//启动线程

6.多线程操作

            Thread t1 = new Thread(new ThreadStart(DataRevThread));                  //开启DataRevThreadt1.Name = "DataRevThread";                                            //线程名字t1.Start();t1.IsBackground = true;                                                  //后台运行

7. UDP接收,解码;

8. emgucv使用;

9. 工厂模式:

ModelFactory MF = new ModelFactory();DM = MF.CreateDataModelFactory_v1(sNeed.ToString());

10.信号量线程间同步

Semaphore TaskSemaphoreData = new Semaphore(0, 2560);           //数据缓存队列缓存区TaskSemaphoreRev.WaitOne();                //等待接收队列

11.Bitmap转换为ImageSource

 [System.Runtime.InteropServices.DllImport("gdi32.dll")]public static extern bool DeleteObject(IntPtr hObject);public static ImageSource ChangeBitmapToImageSource(Bitmap bitmap){IntPtr hBitmap = bitmap.GetHbitmap();ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap,IntPtr.Zero,Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions());if (!DeleteObject(hBitmap)){throw new System.ComponentModel.Win32Exception();}return wpfBitmap;}

12.Queue和list的操作

包括但是不限于以上内容

代码如下:

MainWindow.xaml:

<Fluent:RibbonWindow x:Class="thzSoftware.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:Fluent="urn:fluent-ribbon"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"xmlns:local="clr-namespace:thzSoftware"mc:Ignorable="d"Title="MainWindow" Height="600" Width="800" WindowStartupLocation="CenterScreen" WindowState="Maximized" Background="LightBlue" Closing="RibbonWindow_Closing"><Grid ShowGridLines="True" ><Grid.RowDefinitions><RowDefinition Height="*"></RowDefinition><RowDefinition Height="10*"></RowDefinition><RowDefinition Height="*"></RowDefinition><RowDefinition Height="10*"></RowDefinition></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="*"/><ColumnDefinition Width="*"/><ColumnDefinition Width="*"/><ColumnDefinition Width="*"/><ColumnDefinition Width="*"/><ColumnDefinition Width="*"/><ColumnDefinition Width="*"/><ColumnDefinition Width="*"/></Grid.ColumnDefinitions><Label Grid.Row="0" Grid.Column="0" Name="labelCamera1Status" Content="摄像头连接状态" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Background="LightCyan"/><wfi:WindowsFormsHost Grid.Row="1" Grid.Column="0" Background="LightGray"><wf:PictureBox x:Name="Cam1" /></wfi:WindowsFormsHost><Label Grid.Row="2" Grid.Column="0" Name="labelCamera2Status"  Content="摄像头连接状态" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Background="LightCyan"/><wfi:WindowsFormsHost Grid.Row="3" Grid.Column="0" Background="LightGray"><wf:PictureBox x:Name="Cam2" /></wfi:WindowsFormsHost><Label Grid.Row="0" Grid.Column="1" Name="labelThz1Status"  Content="太赫兹连接状态" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Background="LightCyan"/><Image Grid.Row="1" Grid.Column="1" Name="Thz1"  /><Label Grid.Row="2" Grid.Column="1" Name="labelThz2Status"  Content="太赫兹连接状态" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Background="LightCyan"/><Image Grid.Row="3" Grid.Column="1" Name="Thz2" /><Image Grid.Row="1" Grid.Column="2" Name="Thz1Image1" /><Image Grid.Row="1" Grid.Column="3" Name="Thz1Image2" /><Image Grid.Row="1" Grid.Column="4" Name="Thz1Image3" /><Image Grid.Row="1" Grid.Column="5" Name="Thz1Image4" /><Image Grid.Row="1" Grid.Column="6" Name="Thz1Image5" /><Image Grid.Row="1" Grid.Column="7" Name="Thz1Image6" /><Image Grid.Row="3" Grid.Column="2" Name="Thz2Image1" /><Image Grid.Row="3" Grid.Column="3" Name="Thz2Image2" /><Image Grid.Row="3" Grid.Column="4" Name="Thz2Image3" /><Image Grid.Row="3" Grid.Column="5" Name="Thz2Image4" /><Image Grid.Row="3" Grid.Column="6" Name="Thz2Image5" /><Image Grid.Row="3" Grid.Column="7" Name="Thz2Image6" /></Grid>
</Fluent:RibbonWindow>

MainWindow.xaml.cs

using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Threading;
using MessageBox = System.Windows.MessageBox;
using thzModel;
using System.Drawing;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Xml;
using Emgu.CV;
using Emgu.CV.Structure;
using System.Threading;
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.Windows.Controls;
using Image = System.Windows.Controls.Image;namespace thzSoftware
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Fluent.RibbonWindow{DispatcherTimer Cam1ReconnectTimer, Cam2ReconnectTimer;public MainWindow()
{try{InitializeComponent();Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;init();}catch (Exception ex){MessageBox.Show(ex.StackTrace + ex.Message);LogWrite.logWrite(ex.Message, ex.StackTrace);}}public IntPtr PictureDev1Cam { get { return Cam1.Handle; } }public IntPtr PictureDev2Cam { get { return Cam2.Handle; } }IntPtr Cam1Handle = IntPtr.Zero;IntPtr Cam2Handle = IntPtr.Zero;Camera Camera1 = new Camera();Camera Camera2 = new Camera();static private string Cam1IP = "192.168.1.64";static private string Cam2IP = "192.168.1.61";DataProcess DP1 = null, DP2 = null;object ThreadLockBitmap = new object();List<ImageSource> thzBitmapList1 = new List<ImageSource>();List<ImageSource> thzBitmapList2 = new List<ImageSource>();int snapFlag1 = 0, snapFlag2 = 0;static public String DeviceType{get{return deviceType;}set{deviceType = value;}}static private string deviceType = "tps2000";void init()
{ReadConfigXML();Cam1Handle = PictureDev1Cam;Cam2Handle = PictureDev2Cam;Cam1.SizeMode = PictureBoxSizeMode.Zoom;Cam2.SizeMode = PictureBoxSizeMode.Zoom;Cam1ReconnectTimer = new DispatcherTimer();Cam1ReconnectTimer.Interval = new TimeSpan(0, 0, 3);//定时间隔3秒Cam1ReconnectTimer.Tick += Cam1ReconnectTimer_Tick;//加载事件,敲tab键事件框架可以自己出来Cam1ReconnectTimer.Start();Cam2ReconnectTimer = new DispatcherTimer();Cam2ReconnectTimer.Interval = new TimeSpan(0, 0, 3);//定时间隔3秒Cam2ReconnectTimer.Tick += Cam2ReconnectTimer_Tick;//加载事件,敲tab键事件框架可以自己出来Cam2ReconnectTimer.Start();thzModel.Command.CommandUp(8);//发送启动指令string DeviceIP1 = "192.168.1.110";int LocalPort1 = 8007;DP1 = new DataProcess(DeviceIP1, LocalPort1,0);DP1.ShowEvent1 = DrawControls1;DP1.Start();//启动线程string DeviceIP2 = "192.168.1.120";int LocalPort2 = 8009;DP2 = new DataProcess(DeviceIP2, LocalPort2, 1);DP2.ShowEvent2 = DrawControls2;DP2.Start();//启动线程}private void DrawControls1(Bitmap image, bool isWarning, bool isBlack)
{Image[] iSource = { Thz1Image1, Thz1Image2, Thz1Image3, Thz1Image4, Thz1Image5, Thz1Image6 };Dispatcher.Invoke((Action)delegate{labelThz1Status.Content = "太赫兹连接成功";Thz1.Source = ChangeBitmapToImageSource(image);snapFlag1++;thzBitmapList1.Add(Thz1.Source);if (thzBitmapList1.Count > 6)thzBitmapList1.RemoveAt(0);if (snapFlag1 > 8){snapFlag1 = 0;for (int i = 0; i < thzBitmapList1.Count; i++){iSource[i].Source = thzBitmapList1[i];}}});}[System.Runtime.InteropServices.DllImport("gdi32.dll")]public static extern bool DeleteObject(IntPtr hObject);public static ImageSource ChangeBitmapToImageSource(Bitmap bitmap)
{IntPtr hBitmap = bitmap.GetHbitmap();ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap,IntPtr.Zero,Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions());if (!DeleteObject(hBitmap)){throw new System.ComponentModel.Win32Exception();}return wpfBitmap;}private void DrawControls2(Bitmap image, bool isWarning, bool isBlack)
{Image[] iSource = { Thz2Image1, Thz2Image2, Thz2Image3, Thz2Image4, Thz2Image5, Thz2Image6 };Dispatcher.Invoke((Action)delegate {labelThz2Status.Content = "太赫兹连接成功"; Thz2.Source = ChangeBitmapToImageSource(image);snapFlag2++;thzBitmapList2.Add(Thz2.Source);if (thzBitmapList2.Count > 6)thzBitmapList2.RemoveAt(0);if (snapFlag2 > 8){snapFlag2 = 0;for (int i = 0; i < thzBitmapList2.Count; i++){iSource[i].Source = thzBitmapList2[i];}}});}private void ReadConfigXML()
{try{XmlDocument xmlDoc = new XmlDocument();xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "\\config.xml");XmlNode settingNode = xmlDoc.DocumentElement;XmlElement e = settingNode.SelectSingleNode("DeviceType") as XmlElement;if (e == null){deviceType = "tps2000";}else{deviceType = e.InnerText;}}catch (Exception ex){LogWrite.logWrite(ex.Message, ex.StackTrace);}}private void ConnectCamera(int whitch)
{try{string userName = "admin";string password = "a123456.";int PortCamera = 8000;if (whitch == 1){labelCamera1Status.Content = "摄像头连接中...";Task.Run(() =>{if (!Camera1.ConnectCamera(Cam1IP, PortCamera, userName, password)){Dispatcher.Invoke((Action)delegate { labelCamera1Status.Content = "摄像头连接失败"; });}else{Dispatcher.Invoke((Action)delegate { labelCamera1Status.Content = "摄像头连接成功"; });Camera1.Preview(Cam1Handle);Camera1.AdjustMirrorPara(1);Cam1ReconnectTimer.Stop();}});}else{labelCamera2Status.Content = "摄像头连接中...";Task.Run(() =>{if (!Camera2.ConnectCamera(Cam2IP, PortCamera, userName, password)){Dispatcher.Invoke((Action)delegate { labelCamera2Status.Content = "摄像头连接失败"; });}else{Dispatcher.Invoke((Action)delegate { labelCamera2Status.Content = "摄像头连接成功"; });Camera2.Preview(Cam2Handle);Camera2.AdjustMirrorPara(1);Cam2ReconnectTimer.Stop();}});}}catch (Exception ex){MessageBox.Show(ex.StackTrace + ex.Message);LogWrite.logWrite(ex.Message, ex.StackTrace);}}private void Cam1ReconnectTimer_Tick(object sender, EventArgs e)
{ConnectCamera(1);}private void RibbonWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{thzModel.Command.CommandUp(0);//发送停止指令//Application.Exit();}private void Cam2ReconnectTimer_Tick(object sender, EventArgs e)
{ConnectCamera(2);}}}

其余部分代码太长,不贴了,需要的话自己下载:百度网盘

链接:https://pan.baidu.com/s/1k2F0C-0gXX-tK_32m_ksOA

提取码:abs5

C# WPF项目实战(经典)相关推荐

  1. C# 值得永久收藏的WPF项目实战(经典)

    01 - 简介 之前也写过好多篇CM框架相关的项目实战文章,比如: C# WPF框架Caliburn.Micro快速搭建 C# WPF框架Caliburn.Micro入门实例1 C# WPF MVVM ...

  2. 2022年终结版WPF项目实战合集发布

    前言 在年中, 组织了一个WPF公益视频教程, 在半天内凑集了2W+的费用由此启动了该视频录制计划, 到目前为止, 视频教程已经结束. 在这里非常感谢参与本次教程以及长期支持的同学, 下面将主要讲解关 ...

  3. WPF项目实战合集2——WPF框架

    WPF框架MvvmLight 下载安装插件 更换接口与命令 UI界面委托传递参数 命令的泛型编程 Message消息传递 发送消息 注册并接收消息 MicrosoftToolKitMVVM 继承方法与 ...

  4. C# WPF MVVM项目实战(进阶②)

    这篇文章还是在之前用Caliburn.Micro搭建好的框架上继续做的开发,今天主要是增加了一个用户窗体ImageProcessView,然后通过Treeview切换选择项之后在界面显示不同效果的图片 ...

  5. C# WPF MVVM项目实战(进阶①)

    这篇文章还是在之前用Caliburn.Micro搭建好的框架上继续做的开发,今天主要是增加了一个用户窗体TestFormView,然后通过TabControl,将新增的窗体加载到主界面上进行分页显示, ...

  6. Xamarin.Forms 5.0 项目实战发布!

    活动介绍 本次活动主要是 .NET Xamarin.Forms 移动端项目开发实战教程, 与以往相同, 本次的收入(其它部分会另行说明) 将用于社区公益活动, 不限于: 公益性质的个人/组织机构捐赠 ...

  7. 【项目实战合集】计算机视觉毕业设计项目怎么选,超30个经典案例供你选择...

    每年到了搞毕业设计的时候,很多学生朋友都很头疼,指导老师给不了好题目,自己也没有什么好的想法,怕选的太容易了过不了,怕选的太难了做不出!今年我们在计算机视觉方向出了[超过30个基于Pytorch框架] ...

  8. 浙大博士导师整理:Tensorflow和Pytorch的笔记(包含经典项目实战)

    作为一名AI工程师,掌握一门深度学习框架是必备的生存技能之一. 自 TensorFlow 从 Google 中脱颖而出以来,它在研究和商业领域成为最受欢迎的开源深度学习框架,紧接着 从 Faceboo ...

  9. python从入门到项目实战李兴华网盘_贺胜军Python轻松入门到项目实战【经典完整版】...

    贺胜军Python轻松入门到项目实战课程目录 01_Python基本概述 01_计算机组成_操作系统.avi 02_计算机的进制.avi 03_数据存储单位1.avi 04_编码和解码.avi 05_ ...

最新文章

  1. C语言双链表遍历,插入,删除
  2. 解决QueryTask执行中的网络请求错误
  3. Notepad++插件总结
  4. html标签 对word2vec,自然语言学习——使用word2vec对文本进行情感分析
  5. .NET 指南:实现 Equals 方法
  6. 一个程序员的全部,并不是“技术”!知道为什么只能当码农吗?
  7. ViewPager与Tab结合使用
  8. php cookie突然没,PHP利用Cookie设置用户30分钟未操作自动退出功能
  9. 使用python解析C代码
  10. 拖动获取元素_如何使用HTML5实现多个元素的拖放功能
  11. Clover Configurator 5.17.4.0中文版(四叶草clover配置工具)
  12. python之使用pyaudio录音和格式转化
  13. 第3章-线性概率模型(1)-logistics/probit模型
  14. electron 双击放大事件
  15. 2019 NeurIPS | Graph Transformer Networks
  16. 基于OpenCASCADE自制三维建模软件(六)瓶子模型例程
  17. ROS学习笔记之小乌龟跟随
  18. GPIO与IOMUX
  19. 最小割问题-Karger‘s algorithm
  20. linux对 pow 未定义的引用,未定义引用`pow’和`floor’

热门文章

  1. 关于Session的使用和优化
  2. linux主要系统服务介绍
  3. 实验——Windows常用网络测试命令
  4. python xlsx 大文件_Python这样操作能存储100多万行的xlsx文件!Python让你事半功倍!
  5. 02 JRE与JDK
  6. [Android] 修改ImageView的图片颜色
  7. [snmp++]读取cisco路由交换机信息[一] - 环境搭建
  8. 在Firefox中结合Wolfram Alpha和Google搜索结果
  9. 如何使自己的不和谐机器人
  10. 禁用磁盘检查_如何在Windows上禁用“磁盘空间不足”警告