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
{
Point mouseDownPoint = new Point();
bool isMove = false;
int zoomStep = 60;
int waitSeconds;
private string NGImgfoldPath = "";
private List<string> NGImgPathList;
bool StopFlag = true;
public Form1()
{
InitializeComponent();
this.numericUpDown1.Value = 10;
try
{
this.NGImgfoldPath = File.ReadAllText(@"./ParamPath.txt");
textBox1.Text = this.NGImgfoldPath;
}
catch
{
}
this.dataGridView1.ColumnCount = 1;
dataGridView1.Columns[0].Name = "NG图像路径";
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
}
this.dataGridView2.ColumnCount = 1;
dataGridView2.Columns[0].Name = "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;
textBox1.Text = NGImgfoldPath;
}
try
{
System.IO.File.WriteAllText(@"./ParamPath.txt", NGImgfoldPath, Encoding.UTF8);
}
catch (Exception exp)
{
MessageBox.Show("参数配置路径写入本地失败-失败原因:" + exp.Message);
}
}
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);
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();
}
private void button1_Click(object sender, EventArgs e)
{
this.StopFlag = false;
}
private void Form1_Load(object sender, EventArgs e)
{
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);
};
}
Thread.Sleep(((int)this.numericUpDown1.Value)*1000);
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)
{
try
{
string content1 = dataGridView1.CurrentCell.Value.ToString();
content1 = content1.Substring(0, content1.Length - 1);
pictureBox1.Image = Image.FromFile(content1);
}
catch (Exception exp)
{
}
}
private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
try
{
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)
{
}
}
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/d3f9899d48b14d9294dc8fc50a5bf5cf.png)