Winform实时显示图像桌面程序

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;
        // 监听Flag
        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;
                //MessageBox.Show("已选择文件夹:" + foldPath, "选择文件夹提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox1.Text = NGImgfoldPath;
            }

            try
            {
                //如果文件不存在,则创建;存在则覆盖
                System.IO.File.WriteAllText(@"./ParamPath.txt", NGImgfoldPath, Encoding.UTF8);
            }
            catch (Exception exp)
            {
                MessageBox.Show("参数配置路径写入本地失败-失败原因:" + exp.Message);
            }
        }

        // 开始监听,每隔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();
        }

        // 停止监听
        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);
                        };


                    }
             
                    // 暂停2s
                    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);
                //   MessageBox.Show(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)
            {

            }

           // MessageBox.Show(content);
        }
    }
}

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值