【转载】 C#和halcon实现图片的放大和缩小
e.Delta>0表示鼠标向上滚动,e.Delta<0表示向下滚动
要拖动的图像为Measure.currentImageL,可以更换。
#region 鼠标实现放大缩小,移动图片
//鼠标滚动事件:实现放大和缩小图像
private void WinHandle\_HMouseWheel(object sender, HalconDotNet.HMouseEventArgs e)
{
try
{
HWindowControl WinHandle = sender as HWindowControl;
HObject ho\_currentImage = null;
HOperatorSet.GenEmptyObj(out ho\_currentImage);
if (WinHandle.Name == "WinHandle\_Left")
{
ho\_currentImage = Measure.ho\_CurrentImageL;
}
if (WinHandle.Name == "WinHandle\_Right")
{
ho\_currentImage = Measure.ho\_CurrentImageR;
}
//放大倍数,当前鼠标选择的图像点坐标Row, Col,按下鼠标的左键还是右键:o-没按下,1-左键,2-中键,4-右键
HTuple Zoom, Row, Col, Button;
HTuple RowLeftUpper, ColumnLeftUpper, RowRightLower, ColumnRightLower, Ht, Wt, ImagePartRowLeftUp, ImagePartColLeftUp, ImagePartRowRightLow, ImagePartColRightLow;
//鼠标向上滚动表示放大
if (e.Delta > 0)
{
Zoom = 1.5;
}
//向下滚动缩小
else
{
Zoom = 0.5;
}
//返回输出窗口中鼠标指针和鼠标按钮所按下的像素精确图像坐标,输出当前鼠标指针点的图像坐标以及按下的是鼠标左键还是右键,0是鼠标左键
HOperatorSet.GetMposition(WinHandle.HalconWindow, out Row, out Col, out Button);
//Get part返回窗口中显示的图像部分的左上角和右下角
//得到当前的窗口坐标,Row0:图像部分左上角的行索引,Column0:图像部分左上角的列索引,Row00:图像部分右下角的行索引,Column00:图像部分右下角的列索引
HOperatorSet.GetPart(WinHandle.HalconWindow, 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(WinHandle.HalconWindow, ImagePartRowLeftUp, ImagePartColLeftUp, ImagePartRowRightLow, ImagePartColRightLow);
HOperatorSet.ClearWindow(WinHandle.HalconWindow);
if (ho\_currentImage != null)
{
HOperatorSet.DispObj(ho\_currentImage, WinHandle.HalconWindow);
}
}
}
catch (Exception)
{
}
}
//【1】鼠标按下,准备拖动图像
private void WinHandle\_HMouseDown(object sender, HalconDotNet.HMouseEventArgs e)
{
HWindowControl WinHandle = sender as HWindowControl;
if (WinHandle.Name == "WinHandle\_Left")
{
if (Measure.ho\_CurrentImageL == null)
{
return;
}
}
if (WinHandle.Name == "WinHandle\_Right")
{
if (Measure.ho\_CurrentImageR == null)
{
return;
}
}
HTuple Row, Column, Button;
//返回鼠标当前按下点的图像坐标
HOperatorSet.GetMposition(WinHandle.HalconWindow, out Row, out Column, out Button);
RowDown = Row; //鼠标按下时的行坐标
ColDown = Column; //鼠标按下时的列坐标
}
//【2】鼠标移动,开始拖动图像
private void WinHandle\_HMouseMove(object sender, HMouseEventArgs e)
{
try
{
HWindowControl WinHandle = sender as HWindowControl;
HObject ho\_currentImage = null;
HOperatorSet.GenEmptyObj(out ho\_currentImage);
if (WinHandle.Name == "WinHandle\_Left")
{
ho\_currentImage = Measure.ho\_CurrentImageL;
}
if (WinHandle.Name == "WinHandle\_Right")
{
ho\_currentImage = Measure.ho\_CurrentImageR;
}
HTuple Row = new HTuple(), Column = new HTuple(), Button = new HTuple(), pointGray = new HTuple();
HTuple hv\_Width = new HTuple();
HTuple hv\_Height = new HTuple();
if (ho\_currentImage != null)
{
HOperatorSet.GetImageSize(ho\_currentImage, out hv\_Width, out hv\_Height);
}
//bug
HOperatorSet.GetMposition(WinHandle.HalconWindow, out Row, out Column, out Button);
////获取当前鼠标的坐标值
if (hv\_Height != null && (Row > 0 && Row < hv\_Height) && (Column > 0 && Column < hv\_Width))//设置3个条件项,防止程序崩溃。
{
HOperatorSet.GetGrayval(ho\_currentImage, Row, Column, out pointGray); //获取当前点的灰度值
}
else
{
pointGray = "\_";
}
String str = String.Format("Row:{0} Column:{1} Gray:{2}", Row, Column, pointGray);
ts\_Grval.Text = str;
}
catch (HalconException )
{
}
}
//【3】鼠标抬起,完成拖动图像
private void WinHandle\_HMouseUp(object sender, HalconDotNet.HMouseEventArgs e)
{
try
{
HWindowControl WinHandle = sender as HWindowControl;
HObject ho\_currentImage = null;
HOperatorSet.GenEmptyObj(out ho\_currentImage);
if (WinHandle.Name == "WinHandle\_Left")
{
ho\_currentImage = Measure.ho\_CurrentImageL;
}
if (WinHandle.Name == "WinHandle\_Right")
{
ho\_currentImage = Measure.ho\_CurrentImageR;
}
HTuple row1, col1, row2, col2, Row, Column, Button;
////获取当前鼠标的坐标值
HOperatorSet.GetMposition(WinHandle.HalconWindow, out Row, out Column, out Button);
double RowMove = Row - RowDown; //鼠标弹起时的行坐标减去按下时的行坐标,得到行坐标的移动值
double ColMove = Column - ColDown;//鼠标弹起时的列坐标减去按下时的列坐标,得到列坐标的移动值
//得到当前的窗口坐标
HOperatorSet.GetPart(WinHandle.HalconWindow, out row1, out col1, out row2, out col2);
//移动后的左上角和右下角坐标,这里可能有些不好理解。以左上角原点为参考点
HOperatorSet.SetPart(WinHandle.HalconWindow, row1 - RowMove, col1 - ColMove, row2 - RowMove, col2 - ColMove);
HOperatorSet.ClearWindow(WinHandle.HalconWindow);
if (ho\_currentImage!=null)
HOperatorSet.DispObj(ho\_currentImage, WinHandle.HalconWindow);
}
catch (Exception)
{
}
}
#endregion
原图显示
private void btn_FullWindowLeft_Click(object sender, EventArgs e)
{
HOperatorSet.SetPart(WinHandle_Left.HalconWindow, 0, 0, measure.hv_HeightL - 1, measure.hv_WidthL - 1);
HOperatorSet.ClearWindow(WinHandle_Left.HalconWindow);
HOperatorSet.DispObj(Measure.ho_CurrentImageL, WinHandle_Left.HalconWindow);
if (Measure.ho_CurrentImageL != null)
HOperatorSet.DispObj(Measure.ho_CurrentImageL, WinHandle_Left.HalconWindow);
}
private void btn\_FullWindowRight\_Click(object sender, EventArgs e)
{
HOperatorSet.SetPart(WinHandle\_Right.HalconWindow, 0, 0, measure.hv\_HeightR - 1, measure.hv\_WidthR - 1);
HOperatorSet.ClearWindow(WinHandle\_Right.HalconWindow);
HOperatorSet.DispObj(measure.ho\_ImageRectifiedR, WinHandle\_Right.HalconWindow);
HOperatorSet.DispObj(Measure.ho\_CurrentImageR, WinHandle\_Right.HalconWindow);
if (Measure.ho\_CurrentImageR != null)
HOperatorSet.DispObj(Measure.ho\_CurrentImageR, WinHandle\_Right.HalconWindow);
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章