((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString()
这段代码可以获取到文件拖拽至加载范围后,文件的绝对路径。
下面开始动手(这里以Form窗口为例):
首先设置窗口允许“拖拽加载”功能,AllowDrop = true
然后添加“拖拽”事件和它的方法:
在方法中添加 string filePath = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString()
//filePath即是我们得到的文件绝对路径
完整的演示:
窗口设计:
代码:
using HalconDotNet;
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;
namespace 二维码识别
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//窗口的拖拽事件
private void Form1_DragEnter(object sender, DragEventArgs e)
{
//获取拖拽进来的文件路径
string picPath = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
//以下与主题不干涉
HObject ho_Image = null;
HTuple hv_HalconWindow = this.hWindowControl1.HalconWindow, hv_Width, hv_Height;
try
{
HOperatorSet.ReadImage(out ho_Image, picPath);
}
catch (Exception)
{
MessageBox.Show("文件类型不是图片!");
return;
}
HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
HOperatorSet.SetPart(hv_HalconWindow, 0, 0, hv_Height, hv_Width);
HOperatorSet.DispObj(ho_Image, hv_HalconWindow);
}
}
}
效果: