C# 打开图片文件 Halcon显示
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
namespace DispImage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
HObject ReadImage = null;
Task[] tasks = new Task[100];
Boolean isRunning = false;
private void button2_Click(object sender, EventArgs e)
{
isRunning = false;
}
private async void button1_Click(object sender, EventArgs e)
{
string[] strs = null;
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;
openFileDialog.Title = "选择图片";
openFileDialog.Filter = "all (*.*)|";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
strs = openFileDialog.FileNames;
}
isRunning = true;
tasks[0] = Task.Factory.StartNew(() =>
{
try
{
while (isRunning)
{
for (int i = 0; i < strs.Length; i++)
{
HOperatorSet.ReadImage(out ReadImage, strs[i]);
hWindow_Final1.HobjectToHimage(ReadImage);
Thread.Sleep(500);
hWindow_Final1.ClearWindow();
Thread.Sleep(500);
//label1.Text = DateTime.Now.ToString();
label1.BeginInvoke(new Action(() => { label1.Text = DateTime.Now.ToString(); }));
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "异常");
}
finally
{
}
});
await tasks[0];
}
}
}