视觉521 halcon24.05 测试

Halcon 24.05

安装

重启后安装

配置







测试









csharp 复制代码
<Window x:Class="Zhaoxi.AirCompress.WPFv1.View.Text"
        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:Zhaoxi.AirCompress.WPFv1.View"
        mc:Ignorable="d"
        Title="Text" Height="450" Width="800">
   
    <Grid>
        

        <Button x:Name="btn" Content="ok" Click="btn_Click" Width="110" Height="35" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="20"/>

    </Grid>
</Window>
csharp 复制代码
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Zhaoxi.AirCompress.WPFv1.View
{
    /// <summary>
    /// Text.xaml 的交互逻辑
    /// </summary>
    public partial class Text : Window
    {
        public Text()
        {
            InitializeComponent();
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                HOperatorSet.ReadImage(out HObject image, "fabrik");
                HOperatorSet.GetImageSize(image, out HTuple width, out HTuple height);
                MessageBox.Show($"Halcon调用成功!图像大小:{width}X{height}");

            }
            catch (Exception ex) {
                MessageBox.Show($"调用失败:{ex.Message}");
            }
        }
    }
}
xml 复制代码
<Window x:Class="Zhaoxi.AirCompress.WPFv1.View.Text"
        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:Zhaoxi.AirCompress.WPFv1.View"
        xmlns:halcon="http://schemas.mvtec.com/halcondotnet"
        mc:Ignorable="d"
        Title="Text" Height="450" Width="800">
   
    <Grid>
        <halcon:HWindowControlWPF
            x:Name="HalconViewer"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
            BorderBrush="Gray" BorderThickness="1"/>

        <Button x:Name="btn" Content="ok" Click="btn_Click" Width="110" Height="35" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="20"/>

    </Grid>
</Window>
csharp 复制代码
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Zhaoxi.AirCompress.WPFv1.View
{
    /// <summary>
    /// Text.xaml 的交互逻辑
    /// </summary>
    public partial class Text : Window
    {
        public Text()
        {
            InitializeComponent();
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                HOperatorSet.ReadImage(out HObject image, "fabrik");
                // HOperatorSet.GetImageSize(image, out HTuple width, out HTuple height);
                HalconViewer.HalconWindow.DispImage((HImage)image);
                // HalconViewer.HalconID.OpenWindow(0,0,width,height,"visible","");
                //MessageBox.Show($"Halcon调用成功!图像大小:{width}X{height}");
                //HalconViewer.HalconID.DisImage(image);
                HOperatorSet.SetPart(HalconViewer.HalconID, 0, 0, 511, 511);
            }
            catch (Exception ex) {
                MessageBox.Show($"调用失败:{ex.Message}");
            }
        }
    }
}
csharp 复制代码
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Zhaoxi.AirCompress.WPFv1.View
{
    /// <summary>
    /// Text.xaml 的交互逻辑
    /// </summary>
    public partial class Text : Window
    {
        public Text()
        {
            InitializeComponent();
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                HOperatorSet.ReadImage(out HObject image, "fabrik");
                // HOperatorSet.GetImageSize(image, out HTuple width, out HTuple height);
                HOperatorSet.SetPart(HalconViewer.HalconID, 0, 0, 511, 511);
                HOperatorSet.DispImage(image,HalconViewer.HalconID);
               // HalconViewer.HalconWindow.DispImage((HImage)image);
                // HalconViewer.HalconID.OpenWindow(0,0,width,height,"visible","");
                //MessageBox.Show($"Halcon调用成功!图像大小:{width}X{height}");
                //HalconViewer.HalconID.DisImage(image);
                image.Dispose();
            }
            catch (Exception ex) {
                MessageBox.Show($"调用失败:{ex.Message}");
            }
        }
    }
}
csharp 复制代码
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Zhaoxi.AirCompress.WPFv1.View
{
    /// <summary>
    /// Text.xaml 的交互逻辑
    /// </summary>
    public partial class Text : Window
    {
        public Text()
        {
            InitializeComponent();
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, new Action(() =>
            {
                try
                {
                    HImage image = new HImage("fabrik");
                    //HOperatorSet.ReadImage(out HObject image, "fabrik");
                    // HOperatorSet.GetImageSize(image, out HTuple width, out HTuple height);
                    HOperatorSet.SetPart(HalconViewer.HalconID, 0, 0, 511, 511);
                    //  HOperatorSet.DispImage(image, HalconViewer.HalconID);
                    // HalconViewer.HalconWindow.DispImage((HImage)image);
                    // HalconViewer.HalconID.OpenWindow(0,0,width,height,"visible","");
                    //MessageBox.Show($"Halcon调用成功!图像大小:{width}X{height}");
                    HalconViewer.HalconWindow.DispImage(image);
                    image.Dispose();
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"调用失败:{ex.Message}");
                }
            }));
            
        }
    }
}


为什么用 HalconViewer.HalconWindow.SetPart?

HalconWindow 是 HWindowControlWPF 控件自带的 HALCON 窗口对象,它的 SetPart 方法是专门为 .NET 封装的,参数类型完全匹配,不会出现 #1201 错误。

更重要的是:它和后面的 DispImage 用的是同一个窗口对象,设置的显示区域会直接生效,图像会按你的要求显示。

SetPart 参数解释

SetPart(Row1, Column1, Row2, Column2):设置图像的显示区域,从(Row1, Column1)到(Row2, Column2)。

示例图像 fabrik 是 512×512 的,所以参数是 (0, 0, 511, 511),表示显示整个图像,不会被裁剪或拉伸。

Dispatcher.BeginInvoke 的作用

确保 HWindowControlWPF 控件完全渲染后再执行图像操作,避免窗口句柄未初始化导致的错误。

csharp 复制代码
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Zhaoxi.AirCompress.WPFv1.View
{
    /// <summary>
    /// Text.xaml 的交互逻辑
    /// </summary>
    public partial class Text : Window
    {
        public Text()
        {
            InitializeComponent();
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, new Action(() =>
            {
                try
                {
                    HImage image = new HImage("fabrik");
                    //HOperatorSet.ReadImage(out HObject image, "fabrik");
                    // HOperatorSet.GetImageSize(image, out HTuple width, out HTuple height);
                    HalconViewer.HalconWindow.SetPart(0, 0, 511, 511);
                    //  HOperatorSet.DispImage(image, HalconViewer.HalconID);
                    // HalconViewer.HalconWindow.DispImage((HImage)image);
                    // HalconViewer.HalconID.OpenWindow(0,0,width,height,"visible","");
                    //MessageBox.Show($"Halcon调用成功!图像大小:{width}X{height}");
                    HalconViewer.HalconWindow.DispImage(image);
                    image.Dispose();
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"调用失败:{ex.Message}");
                }
            }));
            
        }
    }
}
xml 复制代码
<Window x:Class="Zhaoxi.AirCompress.WPFv1.View.Text"
        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:Zhaoxi.AirCompress.WPFv1.View"
        xmlns:halcon="http://schemas.mvtec.com/halcondotnet"
        mc:Ignorable="d"
        Title="Text" Height="450" Width="800">
   
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <halcon:HWindowControlWPF
            Grid.Row="0"
            x:Name="HalconViewer"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
            BorderBrush="Gray" BorderThickness="1"/>
        <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" Margin="10">
        <Button x:Name="btn" Content="ok" Click="btn_Click" Width="110"
                Height="35" HorizontalAlignment="Right"
                VerticalAlignment="Bottom" Margin="20"
                Background="#ff2196f3"
                Foreground="White"
                FontSize="14"/>
        </StackPanel>
    </Grid>
</Window>
相关推荐
czhc11400756638 小时前
610:代码报错
mvvm·视觉
czhc11400756633 天前
6.7 弱引用
视觉
搞科研的小刘选手6 天前
【高届数计算机方向会议】第七届计算机视觉与数据挖掘国际学术会议(ICCVDM 2026)
人工智能·算法·计算机·数据挖掘·软件工程·视觉·信息
czhc11400756637 天前
6.3 # GigE Vision:半导体X光检测的高速图像传输协议
视觉
czhc11400756639 天前
5.30:多VM解耦 X光检测(曝光)(func<CancellationToken,Task> scanLogic)
视觉
czhc11400756639 天前
6.1EtherCAT工业架构:软主站,分布式时钟DC,PDO实时通信
视觉·运控
czhc114007566312 天前
529: XYScanAxis()类
mvvm·视觉·运控
czhc114007566313 天前
528:Halcon图像控件 启动轴状态实时监控
mvvm·视觉·运控
czhc114007566314 天前
5.27 :工业设备检测模式与安全防护详解
安全·mvvm·视觉·运控
czhc114007566316 天前
525:检测完善
mvvm·视觉