H S m a r t W i n d o w C o n t r o l HSmartWindowControl HSmartWindowControl
hv_MainWindowHandle5 = hSmartWindowControl.HalconWindow;
public HTuple hv_MainWindowHandle1
MachineMaster.halcon.ShowImageSingle(MachineMaster.halcon.hv_MainWindowHandle1, MachineMaster.halcon.ho_Image1, 1);
public bool ShowImageSingle(HTuple WindowHandle, HObject DispImage, int WindowNo)
{
HOperatorSet.GetImageSize(DispImage, out hv_Width1, out hv_Height1);
HOperatorSet.SetWindowAttr("background_color", "black");
HOperatorSet.SetPart(WindowHandle, 0, 0, hv_Height1, hv_Width1);
}
在Halcon中,为了设置HSmartWindowControl
控件以自适应方式显示图像,确保图像在控件中居中显示并且图像的长宽比例与原图保持一致,可以通过调整图像在控件中的显示区域来实现。以下是一个设置自适应显示大小的步骤和方法,结合了参考文章4中的相关信息:
-
获取控件和图像的尺寸:
- 首先,获取
HSmartWindowControl
控件的宽度(cWidth)和高度(cHeight)。 - 然后,获取要显示的图像(HObject)的宽度(mWidth)和高度(mHeight)。
- 首先,获取
-
计算缩放比例:
- 计算图像在控件中的宽度缩放比例(scaleWidth)和高度缩放比例(scaleHeight)。这两个比例分别是图像宽度和高度与控件宽度和高度的比值。
double scaleWidth = 1.0 * mWidth / cWidth; double scaleHeight = 1.0 * mHeight / cHeight;
-
确定显示区域的坐标:
-
根据缩放比例和控件的尺寸,计算图像在控件中显示的起始坐标(row1, col1)和结束坐标(row2, col2)。这里需要判断是宽度缩放比例大还是高度缩放比例大,以决定是依据宽度还是高度来调整显示区域。
-
如果宽度缩放比例大于高度缩放比例(即图像更宽),则根据控件的高度和图像的宽度缩放比例来计算新的高度(finHeight),并据此确定起始和结束坐标。
if (scaleWidth > scaleHeight) { double finHeight = cHeight * scaleWidth; row1 = -(1.0) * (finHeight - mHeight) / 2; col1 = 0; row2 = row1 + finHeight; col2 = mWidth; // 或者直接使用控件的宽度 cWidth } - 如果高度缩放比例大于宽度缩放比例(即图像更高),则根据控件的宽度和图像的高度缩放比例来计算新的宽度(finWidth),并据此确定起始和结束坐标。 ```csharp else { double finWidth = cWidth * scaleHeight; row1 = 0; col1 = -(1.0) * (finWidth - mWidth) / 2; row2 = cHeight; // 或者直接使用控件的高度 cHeight col2 = col1 + finWidth; }
-
-
设置显示区域并显示图像:
- 使用Halcon的
SetPart
函数设置窗口的显示区域(即上面计算得到的起始和结束坐标)。 - 调用
DispObj
函数将图像显示在控件中。
HOperatorSet.SetPart(hWindow, row1, col1, row2, col2); // hWindow 是控件的窗口句柄 HOperatorSet.DispObj(image, hWindow); // image 是要显示的图像对象
- 使用Halcon的
-
注意事项:
- 在计算坐标时,注意坐标系的原点(0,0)通常位于控件的左上角。
- 在设置显示区域时,确保起始坐标和结束坐标的值在控件的范围内,以避免出现图像显示不全或显示错误的情况。
- 如果需要支持图像的缩放和平移等交互操作,可以进一步添加鼠标事件处理逻辑来更新显示区域。
通过以上步骤,你可以设置HSmartWindowControl
控件以自适应方式显示图像,确保图像在控件中居中显示并且保持原图的长宽比例。
Halcon控件中的HSmartWindowControl
和HWindowControl
在功能和使用上有一些明显的区别。以下是对这两个控件的详细比较:
1. 功能特性
-
HSmartWindowControl:
- 显示优化:更注重显示性能,图片可以缩放和拖拽。
- 鼠标交互:提供了预制的鼠标交互功能,如双击图像会自适应大小,鼠标滚轮控制图像缩放,以及用鼠标选中图像进行移动。
- 集成性:可以像其他控件一样使用,例如嵌入到TabControl和ScrollViewer中,也能被其他控件遮挡。
- 自动调整:控件能无闪烁地自动rescale,在窗口双击后能复位视图。
-
HWindowControl:
- 向后兼容:主要是为了向后兼容而设计的旧控件。
- 基本功能:虽然也提供了图像显示的基本功能,但在交互性和优化方面不如
HSmartWindowControl
。
2. 使用方法
- HSmartWindowControl:在C#的Windows Forms或WPF中使用时,可以通过添加鼠标滚轮事件的回调来实现特定的交互功能。
- HWindowControl:虽然使用方法与
HSmartWindowControl
类似,但由于其较旧的设计,可能在某些方面不如HSmartWindowControl
灵活和方便。
3. 推荐使用
根据参考文章4的建议,强烈建议使用HSmartWindowControl控件,因为它提供了更丰富的功能和更好的用户体验。而HWindowControl
则主要作为向后兼容的选项存在。
总结
HSmartWindowControl
和HWindowControl
在Halcon控件中分别代表了不同的图像显示和控制方案。HSmartWindowControl
以其优化的显示性能、丰富的鼠标交互功能和良好的集成性而备受推荐,而HWindowControl
则主要面向需要向后兼容的场景。在设计和开发基于Halcon的图像处理应用时,建议优先考虑使用HSmartWindowControl
控件。
加滚轮,放大缩小图像
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 图像分析软件.Forms
{
public partial class FrmStep1 : Form
{
// 一 halcon引擎
public HDevEngine MyEngine = new HDevEngine();
public FrmStep1()
{
InitializeComponent();
// 二 halcon引擎初始化halcon文件位置
MyEngine.SetProcedurePath(@".\HalconAlgo");
hSmartWindowControl1.MouseWheel += HSmartWindowControl1_MouseWheel;
}
private void HSmartWindowControl1_MouseWheel(object sender, MouseEventArgs e)
{
double zoomFactor = 1.0; // 缩放因子,可以根据需要调整
if (e.Delta > 0)
{
// 向上滚动,放大图像
zoomFactor = 1.1; // 例如放大10%
}
else
{
// 向下滚动,缩小图像
zoomFactor = 0.9; // 例如缩小10%
}
// 获取当前窗口的显示区域
HTuple row1, col1, row2, col2;
hSmartWindowControl1.HalconWindow.GetPart(out row1, out col1, out row2, out col2);
// 计算新的显示区域(这里只是简单地将所有坐标乘以缩放因子,实际可能需要根据图像和窗口大小进行调整)
double newRow1 = row1[0] * zoomFactor;
double newCol1 = col1[0] * zoomFactor;
double newRow2 = row2[0] * zoomFactor;
double newCol2 = col2[0] * zoomFactor;
// 设置新的显示区域
hSmartWindowControl1.HalconWindow.SetPart(newRow1, newCol1, newRow2, newCol2);
}
private void btnExec_Click(object sender, EventArgs e)
{
// 加载图像
HObject ho_Image = null;
string item_imgPath = @"D:\0626收集图像\085020591溢胶\1.tif";
HOperatorSet.ReadImage(out ho_Image, item_imgPath);
hSmartWindowControl1.HalconWindow.DispObj(ho_Image);
}
}
}
改变显示模式
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 图像分析软件.Forms
{
public partial class FrmStep1 : Form
{
// 一 halcon引擎
public HDevEngine MyEngine = new HDevEngine();
public FrmStep1()
{
InitializeComponent();
// 二 halcon引擎初始化halcon文件位置
MyEngine.SetProcedurePath(@".\HalconAlgo");
hSmartWindowControl1.MouseWheel += HSmartWindowControl1_MouseWheel;
// 设置窗口显示模式
hSmartWindowControl1.HalconWindow.SetDraw("margin");
// 设置字体显示颜色
hSmartWindowControl1.HalconWindow.SetColor("red");
// 设置显示轮廓线宽
hSmartWindowControl1.HalconWindow.SetLineWidth(2);
}
private void HSmartWindowControl1_MouseWheel(object sender, MouseEventArgs e)
{
double zoomFactor = 1.0; // 缩放因子,可以根据需要调整
if (e.Delta > 0)
{
// 向上滚动,放大图像
zoomFactor = 1.1; // 例如放大10%
}
else
{
// 向下滚动,缩小图像
zoomFactor = 0.9; // 例如缩小10%
}
// 获取当前窗口的显示区域
HTuple row1, col1, row2, col2;
hSmartWindowControl1.HalconWindow.GetPart(out row1, out col1, out row2, out col2);
// 计算新的显示区域(这里只是简单地将所有坐标乘以缩放因子,实际可能需要根据图像和窗口大小进行调整)
double newRow1 = row1[0] * zoomFactor;
double newCol1 = col1[0] * zoomFactor;
double newRow2 = row2[0] * zoomFactor;
double newCol2 = col2[0] * zoomFactor;
// 设置新的显示区域
hSmartWindowControl1.HalconWindow.SetPart(newRow1, newCol1, newRow2, newCol2);
}
private void btnExec_Click(object sender, EventArgs e)
{
// 加载图像
HObject ho_Image = null;
HObject ho_rect = null;
string item_imgPath = @"D:\0626收集图像\085020591溢胶\1.tif";
HOperatorSet.ReadImage(out ho_Image, item_imgPath);
HOperatorSet.GenRectangle1(out ho_rect, 100, 100, 500, 500);
hSmartWindowControl1.HalconWindow.DispObj(ho_Image);
hSmartWindowControl1.HalconWindow.DispObj(ho_rect);
}
}
}