【C#】WPF实现HaIcon图像缩放、移动
1.HaIcon实现的C#代码
//
// File generated by HDevelop for HALCON/DOTNET (C#) Version 12.0
//
// This file is intended to be used with the HDevelopTemplate or
// HDevelopTemplateWPF projects located under %HALCONEXAMPLES%\c#
using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using HalconDotNet;
public partial class HalconImageHandle
{
HTuple? hv_ExpDefaultWinHandle, RowDown, ColDown;
HObject? ho_Image;
bool isMouseDown = false;
/// <summary>
/// 鼠标按下
/// </summary>
public void MouseDown()
{
try
{
HTuple Row, Column, Button;
//返回鼠标当前按下点的图像坐标
HOperatorSet.GetMposition(hv_ExpDefaultWinHandle, out Row, out Column, out Button);
RowDown = Row; //鼠标按下时的行坐标
ColDown = Column; //鼠标按下时的列坐标
if (Button.I == 1)//左键嗯下值为1
isMouseDown = true;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 鼠标提起
/// </summary>
public void MouseUp()
{
try
{
if (isMouseDown)
{
isMouseDown = false;
}
}
catch (Exception)
{
}
}
/// <summary>
/// 移动
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
public void imagetrans()
{
try
{
if (!isMouseDown)
{
return;
}
HTuple Row, Column, Button;
//获取当前鼠标的坐标值
HOperatorSet.GetMposition(hv_ExpDefaultWinHandle, out Row, out Column, out Button);
double RowMove = Row - RowDown; //鼠标弹起时的行坐标减去按下时的行坐标,得到行坐标的移动值
double ColMove = Column - ColDown;//鼠标弹起时的列坐标减去按下时的列坐标,得到列坐标的移动值
HTuple row1, col1, row2, col2;
//得到当前的窗口坐标
HOperatorSet.GetPart(hv_ExpDefaultWinHandle, out row1, out col1, out row2, out col2);
//移动后的左上角和右下角坐标,这里可能有些不好理解。以左上角原点为参考点
HOperatorSet.SetPart(hv_ExpDefaultWinHandle, row1 - RowMove, col1 - ColMove, row2 - RowMove, col2 - ColMove);
HOperatorSet.ClearWindow(hv_ExpDefaultWinHandle);
HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 缩放
/// </summary>
/// <param name="Zoom"></param>
public void imagescale(HTuple Zoom)
{
try
{
//放大倍数,当前鼠标选择的图像点坐标Row, Col,按下鼠标的左键还是右键:o-没按下,1-左键,2-中键,4-右键
HTuple Row, Col, Button;
HTuple RowLeftUpper, ColumnLeftUpper, RowRightLower, ColumnRightLower, Ht, Wt, ImagePartRowLeftUp, ImagePartColLeftUp, ImagePartRowRightLow, ImagePartColRightLow;
//返回输出窗口中鼠标指针和鼠标按钮所按下的像素精确图像坐标,输出当前鼠标指针点的图像坐标以及按下的是鼠标左键还是右键,0是鼠标左键
HOperatorSet.GetMposition(hv_ExpDefaultWinHandle, out Row, out Col, out Button);
//Get part返回窗口中显示的图像部分的左上角和右下角
//得到当前的窗口坐标,Row0:图像部分左上角的行索引,Column0:图像部分左上角的列索引,Row00:图像部分右下角的行索引,Column00:图像部分右下角的列索引
HOperatorSet.GetPart(hv_ExpDefaultWinHandle, out RowLeftUpper, out ColumnLeftUpper, out RowRightLower, out ColumnRightLower);
//显示的部分图像的高
Ht = RowRightLower - RowLeftUpper;
//显示的部分图像的宽
Wt = ColumnRightLower - ColumnLeftUpper;
//普通版halcon能处理的图像最大尺寸是32K*32K。如果无限缩小原图像,导致显示的图像超出限制,则会造成程序崩溃
if (Ht * Wt < 32000 * 32000 || Zoom == 1.5)
{
//显示的放大或者缩小部分图像的左上角和右下角坐标
ImagePartRowLeftUp = (RowLeftUpper + ((1 - (1.0 / Zoom)) * (Row - RowLeftUpper)));
ImagePartColLeftUp = (ColumnLeftUpper + ((1 - (1.0 / Zoom)) * (Col - ColumnLeftUpper)));
ImagePartRowRightLow = ImagePartRowLeftUp + (Ht / Zoom);
ImagePartColRightLow = ImagePartColLeftUp + (Wt / Zoom);
//设置部分显示图像
HOperatorSet.SetPart(hv_ExpDefaultWinHandle, ImagePartRowLeftUp, ImagePartColLeftUp, ImagePartRowRightLow, ImagePartColRightLow);
HOperatorSet.ClearWindow(hv_ExpDefaultWinHandle);
HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
}
}
catch (Exception)
{
throw;
}
}
// Main procedure
private void action(String filePath)
{
HOperatorSet.GenEmptyObj(out ho_Image);
HOperatorSet.ReadImage(out ho_Image, filePath);
HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
}
public void InitHalcon()
{
// Default settings used in HDevelop
HOperatorSet.SetSystem("width", 512);
HOperatorSet.SetSystem("height", 512);
}
public void RunHalcon(HTuple Window, String filePath)
{
hv_ExpDefaultWinHandle = Window;
action(filePath);
}
public void Dispose()
{
ho_Image?.Dispose();
}
}
2.WPF实现
2.1控件引用
<halcon:HWindowControlWPF
x:Name="HW_Gerber"
Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="2"
Margin="1,0,1,1"
HMouseDown="HW_Gerber_HMouseDown"
HMouseMove="HW_Gerber_HMouseMove"
HMouseUp="HW_Gerber_HMouseUp"
HMouseWheel="HW_Gerber_HMouseWheel" />
2.2后台cs实现
private void HW_Gerber_HMouseDown(object sender, HMouseEventArgsWPF e)
{
try
{
boardTestImageViewModel?.MouseDown();
}
catch (Exception)
{
}
}
private void HW_Gerber_HMouseMove(object sender, HMouseEventArgsWPF e)
{
try
{
boardTestImageViewModel?.MouseMove();
}
catch (Exception)
{
}
}
private void HW_Gerber_HMouseWheel(object sender, HMouseEventArgsWPF e)
{
try
{
HTuple Zoom;
//鼠标向上滚动表示放大
if (e.Delta > 0)
{
Zoom = 1.2;
}
//向下滚动缩小
else
{
Zoom = 0.8;
}
boardTestImageViewModel?.MouseWheel(Zoom);
}
catch (Exception)
{
}
}
private void HW_Gerber_HMouseUp(object sender, HMouseEventArgsWPF e)
{
boardTestImageViewModel?.MouseUp();
}
2.3后台viewmodel实现
HalconImageHandle imageHandle = new HalconImageHandle();
/// <summary>
/// 加载图像
/// </summary>
/// <param name="fileName"></param>
private void LoadImage(string fileName)
{
Task.Factory.StartNew(() =>
{
Thread.Sleep(1000);
string filePath = Path.Combine(DirPath, fileName);
imageHandle.RunHalcon(page?.HW_Gerber.HalconWindow, filePath);
});
}
/// <summary>
/// 图像缩放
/// </summary>
/// <param name="Zoom"></param>
public void MouseWheel(HTuple Zoom)
{
imageHandle.imagescale(Zoom);
}
/// <summary>
/// 鼠标移动
/// </summary>
public void MouseMove()
{
imageHandle.imagetrans();
}
/// <summary>
/// 鼠标按下
/// </summary>
public void MouseDown()
{
imageHandle.MouseDown();
}
/// <summary>
/// 鼠标放开
/// </summary>
public void MouseUp()
{
imageHandle.MouseUp();
}