C#实现文件的拖拽加载

((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);
        }
    }
}

效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值