Winform实时显示图像桌面程序
生活随笔
收集整理的這篇文章主要介紹了
Winform实时显示图像桌面程序
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Winform實時顯示圖像桌面程序
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms;namespace WindowsFormsApp1 {public partial class Form1 : Form{//Bitmap myBmp;Point mouseDownPoint = new Point(); //記錄拖拽過程鼠標位置bool isMove = false; //判斷鼠標在picturebox上移動時,是否處于拖拽過程(鼠標左鍵是否按下)int zoomStep = 60; //縮放步長int waitSeconds;// NG圖像目錄路徑private string NGImgfoldPath = "";// NG圖像路徑存儲列表private List<string> NGImgPathList;// 監(jiān)聽Flagbool StopFlag = true;public Form1(){InitializeComponent();this.numericUpDown1.Value = 10;try{this.NGImgfoldPath = File.ReadAllText(@"./ParamPath.txt");textBox1.Text = this.NGImgfoldPath;}catch{}// 列的設(shè)置this.dataGridView1.ColumnCount = 1;dataGridView1.Columns[0].Name = "NG圖像路徑";// 列寬自適應(yīng)foreach (DataGridViewColumn column in dataGridView1.Columns){column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;}// 列的設(shè)置this.dataGridView2.ColumnCount = 1;dataGridView2.Columns[0].Name = "NG圖像目錄路徑";// 列寬自適應(yīng)foreach (DataGridViewColumn column in dataGridView2.Columns){column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;}}// 文件夾按時間排序private void SortAsFolderCreationTime(ref DirectoryInfo[] dirs){Array.Sort(dirs, delegate (DirectoryInfo x, DirectoryInfo y) { return x.CreationTime.CompareTo(y.CreationTime); });}// 文件夾按逆時間排序private void SortAsFolderNiCreationTime(ref DirectoryInfo[] dirs){Array.Sort(dirs, delegate (DirectoryInfo x, DirectoryInfo y) { return y.CreationTime.CompareTo(x.CreationTime); });}string ParamPath = "";// 確定圖像存儲目錄private void button5_Click(object sender, EventArgs e){// 打開文件目錄if (folderBrowserDialog1.ShowDialog() == DialogResult.OK){NGImgfoldPath = folderBrowserDialog1.SelectedPath;//MessageBox.Show("已選擇文件夾:" + foldPath, "選擇文件夾提示", MessageBoxButtons.OK, MessageBoxIcon.Information);textBox1.Text = NGImgfoldPath;}try{//如果文件不存在,則創(chuàng)建;存在則覆蓋System.IO.File.WriteAllText(@"./ParamPath.txt", NGImgfoldPath, Encoding.UTF8);}catch (Exception exp){MessageBox.Show("參數(shù)配置路徑寫入本地失敗-失敗原因:" + exp.Message);}}// 開始監(jiān)聽,每隔2s更新最新的圖像進行顯示private void button2_Click(object sender, EventArgs e){this.StopFlag = true;Action action = () =>{while (StopFlag){string[] files = Directory.GetFiles(@NGImgfoldPath + "\\", "*.*", System.IO.SearchOption.TopDirectoryOnly);List<string> fileNameList = new List<string>();foreach (var f in files){Console.WriteLine(f);//string fileName = System.IO.Path.GetFileName(f); //獲取文件名//fileName = fileName.Substring(0, fileName.IndexOf('.')); // 去除尾綴if (this.dataGridView1.InvokeRequired){Action action2 = () =>{this.dataGridView1.Rows.Add(f+'\n');};this.dataGridView1.Invoke(action2);};if (this.pictureBox1.InvokeRequired){Action action3 = () =>{pictureBox1.Image = Image.FromFile(f);};this.pictureBox1.Invoke(action3);};};Thread.Sleep(2000);break;}};Task task = new Task(action);task.Start();}// 停止監(jiān)聽private void button1_Click(object sender, EventArgs e){this.StopFlag = false;}private void Form1_Load(object sender, EventArgs e){// 設(shè)置顯示高度this.Height = 850;this.StopFlag = true;Action action = () =>{while (StopFlag){// 獲取文件目錄DirectoryInfo di = new DirectoryInfo(@NGImgfoldPath + "\\");// 獲取目錄下所有目錄DirectoryInfo[] arrDir = di.GetDirectories();// 按逆時間排序SortAsFolderNiCreationTime(ref arrDir);// 刪除所有if (this.dataGridView2.InvokeRequired){Action action2 = () =>{this.dataGridView2.Rows.Clear();};this.dataGridView1.Invoke(action2);};// 顯示目錄foreach (var item in arrDir){// 顯示路徑if (this.dataGridView2.InvokeRequired){Action action2 = () =>{this.dataGridView2.Rows.Add(item.Name + '\n');};this.dataGridView1.Invoke(action2);};}// 圖像文件路徑string[] files = Directory.GetFiles(arrDir[0].FullName + "\\", "*.*", System.IO.SearchOption.AllDirectories);// 清除if (this.dataGridView1.InvokeRequired){Action action2 = () =>{this.dataGridView1.Rows.Clear();};this.dataGridView2.Invoke(action2);};foreach (var f in files){// 顯示路徑if (this.dataGridView1.InvokeRequired){Action action2 = () =>{this.dataGridView1.Rows.Add(f + '\n');};this.dataGridView2.Invoke(action2);};// 顯示圖像if (this.pictureBox1.InvokeRequired){Action action3 = () =>{pictureBox1.Image = Image.FromFile(arrDir[0].FullName + "\\Holes\\1.jpg");};this.pictureBox1.Invoke(action3);};}// 暫停2sThread.Sleep(((int)this.numericUpDown1.Value)*1000);// 如果停止監(jiān)聽,則停止監(jiān)聽if (!StopFlag){break;}}};Task task = new Task(action);task.Start();}private void pictureBox1_MouseDown(object sender, MouseEventArgs e){if (e.Button == MouseButtons.Left){mouseDownPoint.X = Cursor.Position.X;mouseDownPoint.Y = Cursor.Position.Y;isMove = true;pictureBox1.Focus();}}private void pictureBox1_MouseUp(object sender, MouseEventArgs e){if (e.Button == MouseButtons.Left){isMove = false;}}private void pictureBox1_MouseMove(object sender, MouseEventArgs e){pictureBox1.Focus();if (isMove){int x, y;int moveX, moveY;moveX = Cursor.Position.X - mouseDownPoint.X;moveY = Cursor.Position.Y - mouseDownPoint.Y;x = pictureBox1.Location.X + moveX;y = pictureBox1.Location.Y + moveY;pictureBox1.Location = new Point(x, y);mouseDownPoint.X = Cursor.Position.X;mouseDownPoint.Y = Cursor.Position.Y;}}//鼠標滾輪滾動功能private void pictureBox1_MouseWheel(object sender, MouseEventArgs e){int x = e.Location.X;int y = e.Location.Y;int ow = pictureBox1.Width;int oh = pictureBox1.Height;int VX, VY;if (e.Delta > 0){pictureBox1.Width += zoomStep;pictureBox1.Height += zoomStep;PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |BindingFlags.NonPublic);Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);pictureBox1.Width = rect.Width;pictureBox1.Height = rect.Height;}if (e.Delta < 0){if (pictureBox1.Width < pictureBox1.Image.Width / 10)return;pictureBox1.Width -= zoomStep;pictureBox1.Height -= zoomStep;PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |BindingFlags.NonPublic);Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);pictureBox1.Width = rect.Width;pictureBox1.Height = rect.Height;}VX = (int)((double)x * (ow - pictureBox1.Width) / ow);VY = (int)((double)y * (oh - pictureBox1.Height) / oh);pictureBox1.Location = new Point(pictureBox1.Location.X + VX, pictureBox1.Location.Y + VY);}private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e){}private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e){//獲取單元格內(nèi)容try{string content1 = dataGridView1.CurrentCell.Value.ToString();content1 = content1.Substring(0, content1.Length - 1);pictureBox1.Image = Image.FromFile(content1);// MessageBox.Show(content1);}catch (Exception exp){}}private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e){try{//獲取單元格內(nèi)容string content = dataGridView2.CurrentCell.Value.ToString();content = content.Substring(0, content.Length - 1);string[] files = Directory.GetFiles(@NGImgfoldPath + "\\" + content + "\\", "*.*", System.IO.SearchOption.AllDirectories);this.dataGridView1.Rows.Clear();foreach (var f in files){// 顯示路徑this.dataGridView1.Rows.Add(f + '\n');}pictureBox1.Image = Image.FromFile(files[0]);}catch (Exception exp){}// MessageBox.Show(content);}} }總結(jié)
以上是生活随笔為你收集整理的Winform实时显示图像桌面程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 双色球系统(JAVA代码)
- 下一篇: ASEMI肖特基二极管10V45参数,1