C# WPF项目实战(经典)
生活随笔
收集整理的這篇文章主要介紹了
C# WPF项目实战(经典)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目的:輸出兩臺攝像頭圖像和兩路設備圖像,每一路設備截圖6張
主要知識:
1. 通過SDK調取攝像頭圖像,并對圖像進行剪裁;
2. WPF中定時器DispatcherTimer用法;
3.?WPF中跨線程訪問控件方法
? Dispatcher.Invoke((Action)delegate {});
區別于winform中
this.Invoke((Action)delegate {});
4.xml操作:
5. 帶多個參數的委托
6.多線程操作
Thread t1 = new Thread(new ThreadStart(DataRevThread)); //開啟DataRevThreadt1.Name = "DataRevThread"; //線程名字t1.Start();t1.IsBackground = true; //后臺運行7. UDP接收,解碼;
8. emgucv使用;
9. 工廠模式:
10.信號量線程間同步
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项目实战(经典)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 撸码是需要直觉的
- 下一篇: NET问答: 如何在 ASP.NET C