C#和Halcon交互实现图片的放大和缩小
【转载】 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); }
4556