ImageControl.Avalonia 1.0.2

dotnet add package ImageControl.Avalonia --version 1.0.2
                    
NuGet\Install-Package ImageControl.Avalonia -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ImageControl.Avalonia" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ImageControl.Avalonia" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="ImageControl.Avalonia" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ImageControl.Avalonia --version 1.0.2
                    
#r "nuget: ImageControl.Avalonia, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ImageControl.Avalonia@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ImageControl.Avalonia&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=ImageControl.Avalonia&version=1.0.2
                    
Install as a Cake Tool

ImageControl.Avalonia

基于 Avalonia 12 / .NET 10 的图像显示控件,面向机器视觉与图像处理场景。

功能

  • 缩放 / 平移:滚轮以鼠标为中心缩放,左键(平移模式)或中键拖拽平移;支持适应窗口、1:1 显示
  • 像素信息:鼠标移动时实时触发 PointerPixelChanged 事件,携带图像坐标与像素值(支持 Gray8 / Gray16 / Gray32Float / Bgra8888 / Rgba8888 / Rgb24 / Rgb565 / Rgba64 / BlackWhite 等格式)
  • ROI 绘制:矩形、圆形、椭圆(可旋转)、圆环扇形、多边形、可旋转矩形、直线;支持选择、整体移动、手柄调整形状(矩形角/边手柄、圆/椭圆半径手柄、圆环扇形内外半径与起止角手柄、直线端点、多边形顶点、旋转矩形角点)、旋转矩形/椭圆旋转手柄(Shift 吸附 15°)、Delete 删除
  • 任意形状显示:GeometryRoi 可包装任意 Avalonia Geometry(SVG 路径字符串、点集、贝塞尔曲线、组合图形等),支持整体拖动与命中测试
  • 开放/多条轮廓显示:AddContour / AddContours 一行显示 HALCON XLD 等算法输出的开放折线或多段轮廓,轮廓之间不会产生错误连线
  • ROI 属性:每个 ROI 提供强类型几何属性(中心、宽高、半径、角度、面积、周长、顶点、质心等),以及 GetProperties() 可读属性列表用于界面展示

快速开始

<Window xmlns:ic="using:ImageControl.Avalonia.Controls">
    <ic:ImageView x:Name="View" ShowCrosshair="True"/>
</Window>
using ImageControl.Avalonia.Controls;
using ImageControl.Avalonia.Roi;

// 设置图像(自动适应窗口)
View.Source = new Bitmap("image.png");

// 鼠标位置与像素值
View.PointerPixelChanged += (s, e) =>
{
    if (e.HasPixel)
        Console.WriteLine($"({e.X}, {e.Y}) = {e.Pixel}");
};

// 切换交互模式:None(平移/编辑 ROI) / Rectangle / RotatedRectangle / Circle / Ellipse / AnnularSector / Polygon / Line
View.DrawMode = RoiDrawMode.Rectangle;

// 获取 ROI 属性
foreach (var roi in View.Rois)
{
    if (roi is RotatedRectangleRoi rr)
        Console.WriteLine($"中心={rr.Center} 宽={rr.Width} 高={rr.Height} 角度={rr.Angle}° 面积={rr.Area}");
}

// 图像处理后显示缺陷区域(边缘点集 → 红色描边 + 半透明填充)
View.AddRegion(defectContourPoints, name: "缺陷 1");

// 显示开放轮廓/多条轮廓(如 HALCON XLD 边缘):默认不闭合、不填充,轮廓之间不会连线
View.AddContour(contourPoints, closed: false, name: "边缘 1");
View.AddContours(contourList, closed: false, name: "XLD 轮廓");

// 编程方式添加 ROI
View.Rois.Add(new CircleRoi(new Point(100, 100), 50) { Stroke = Colors.Red });
View.Rois.Add(new AnnularSectorRoi(new Point(200, 150), 40, 100, -45, 90));

// 显示任意形状(SVG 路径 / 点集 / 任意 Geometry,图像坐标系)
View.Rois.Add(GeometryRoi.FromPathData("M 0,0 L 100,0 L 50,80 Z"));
View.Rois.Add(GeometryRoi.FromPoints(new[] { new Point(10, 10), new Point(80, 20), new Point(40, 60) }));

交互说明

模式 操作
平移 (None) 空白处左键/中键拖拽平移,滚轮缩放

提示:任何模式下,鼠标移到已有 ROI 上即可直接编辑——本体拖动移动,手柄拖拽调整形状,旋转矩形拖旋转手柄旋转(Shift 吸附 15°),Delete 删除选中 ROI;点在空白处才执行当前模式动作(平移或绘制)。 | 矩形 / 椭圆 / 直线 | 按住左键拖拽,Esc 取消 | | 圆 | 从圆心拖到边缘 | | 旋转矩形 | 与矩形一致拖拽对角(初始角度 0),绘制后拖旋转手柄旋转 | | 圆环扇形(Arc 方式) | 从圆心拖出中心圆弧,默认半宽 40、扫掠角 180°;方形端点重定圆心和半径,中心圆弧上的圆点通过三点定圆,外圆弧上的圆点只调整圆环宽度 | | 多边形 | 单击加顶点,鼠标移到首点出现高亮提示后单击即首尾闭合(也可双击 / 右键 / 回车闭合),Esc 取消 |

主要 API

  • ImageView.Source — 显示的 Bitmap,设置后自动适应窗口
  • ImageView.Zoom / Offset / SetZoom / ZoomAt / ZoomToFit / ZoomToActualSize
  • ImageView.ImageToScreen / ScreenToImage — 坐标变换
  • ImageView.TryGetPixel(x, y, out PixelValue) — 像素查询
  • ImageView.Rois / SelectedRoi / RemoveSelectedRoi / ClearRois
  • ImageView.AddRegion(points, stroke, fill, name) — 一行显示边缘点集围成的区域(缺陷显示)
  • ImageView.AddContour(points, ..., closed: false) — 一行显示单条开放/闭合轮廓
  • ImageView.AddContours(contours, ..., closed: false) — 一次显示多条轮廓,轮廓之间不会连线
  • 事件:PointerPixelChanged / RoiAdded / RoiRemoved / SelectedRoiChanged

分层显示

渲染分为两层,与添加顺序无关:

  • 下层(区域层):所有 GeometryRoi(缺陷区域等任意形状显示),批量渲染
  • 上层(ROI 层):矩形、圆、椭圆、多边形、旋转矩形、直线等交互式 ROI

命中测试同样分层:点击重叠区域时优先选中上层的交互式 ROI。

大规模区域显示性能

针对成千上万个缺陷区域的场景已做优化(建议通过 AddRegion 添加,其默认锁定;多条轮廓可用 AddContours 合成一个锁定 ROI):

  • 锁定区域批量渲染:所有锁定的 GeometryRoi 共享一次坐标变换(平移在数据变更时烘焙进渲染副本)
  • 合并渲染(最大收益):相同样式的锁定区域合并为一个 GeometryGroup,一次 DrawGeometry 画完成千上万个区域(N 次绘制调用 → 每种样式 1 次);区域集合或样式不变时不重建分组。可用 MergeLockedRegions="False" 关闭
  • 不可变(冻结)画刷/画笔:所有 ROI 的描边/填充缓存为 ImmutablePen/ImmutableSolidColorBrush——渲染器可直接共享,无变更追踪开销,属性变化时才重建
  • 视口剔除:可见范围外的 ROI 直接跳过
  • 未开十字线时鼠标移动不触发重绘;锁定区域不参与命中测试
  • 注意:批量渲染路径中锁定区域的描边宽度随缩放等比变化(与图像像素对齐);合并渲染后半透明填充的重叠区域不叠加变深(颜色更均匀,通常更适合缺陷显示)

实测 2000 个缺陷区域下平移、缩放流畅。

其他可考虑但暂未实现的方向:区域的空间索引(Bounds 剔除已是 O(N) 轻量比较,收益有限)、整帧离屏位图缓存(平移/缩放每帧内容都变,收益有限)。

许可

MIT

更新日志

版本变更见 CHANGELOG.md。

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 95 11/7/2026
1.0.1 112 10/27/2026
1.0.0 101 10/26/2026

1.0.2:新增 AddContour / AddContours 开放/多轮廓便捷 API,新增 GeometryRoi.FromContours;更新 README、使用说明书与示例。