【halcon】.Net6 wpf与Halcon

新建一个.net6的wpf工程

 右键依赖项,选择添加项目引用

 点击浏览,选择添加halcondotnet.dll:

 前台添加关键代码:

伪代码部分:
xmlns:halcon="clr-namespace:HalconDotNet;assembly=halcondotnet"
<Grid>
    <halcon:HSmartWindowControlWPF   Loaded="HSmartWindowControlWPF_Loaded"/>
</Grid>

-------------完整代码部分--------------

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        xmlns:halcon="clr-namespace:HalconDotNet;assembly=halcondotnet"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <halcon:HSmartWindowControlWPF   Loaded="HSmartWindowControlWPF_Loaded"/>
    </Grid>
</Window>

 后台代码:(注意准备一张1.png的图片放到exe的目录下

private void HSmartWindowControlWPF_Loaded(object sender, RoutedEventArgs e)
{
    var pictureCtrl = sender as HSmartWindowControlWPF;

    HImage img = new HImage("1.png");
    pictureCtrl.HalconWindow.DispImage(img);
}

效果展示

一个关于取名的报错 

目前还有一个问题:

如果你尝试,给HSmartWindowControlWPF 取个名字:

<halcon:HSmartWindowControlWPF x:Name="pictureCtrl"  Loaded="HSmartWindowControlWPF_Loaded"/>

此时报错:

错误    MC1000    未知的生成错误“Could not find assembly 'System.Drawing.Common, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Either explicitly load this assembly using a method such as LoadFromAssemblyPath() or use a MetadataAssemblyResolver that returns a valid assembly. 行 11 位置 40.”   

接下来,我们需要安装包 System.Drawing.Common 就能解决上述问题。

posted @ 2022-10-05 14:01  宋桓公  阅读(955)  评论(0编辑  收藏  举报