【轉載】 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); }