C#與halcon聯(lián)合編程之窗體內(nèi)圖像常見操作
|
admin
2023年2月27日 11:44
本文熱度 1076
|
目錄:讀取圖片
拉伸顯示
不拉伸,原圖比例顯示
圖像縮放
圖像移動(dòng),實(shí)時(shí)移動(dòng)
圖像自適應(yīng)
讀取圖片://獲取文件路徑下的圖片 public HObject GetImgfromPath(string imgPath) { HObject L_Img; HOperatorSet.GenEmptyObj(out L_Img);//清空?qǐng)D片 L_Img.Dispose();//釋放 HOperatorSet.ReadImage(out L_Img, imgPath);//讀取圖片存入到l_img return L_Img; }
拉伸顯示//圖片拉伸顯示 public void ImgIsStretchDisplay(HObject L_Img, HTuple Hwindow) { HTuple hv_Width,hv_Height; HOperatorSet.ClearWindow(Hwindow);//清空畫面 HOperatorSet.GetImageSize(L_Img, out hv_Width, out hv_Height);//獲取圖片大小規(guī)格 HOperatorSet.SetPart(Hwindow, 0, 0, hv_Height, hv_Width);//設(shè)置窗體的規(guī)格 HOperatorSet.DispObj(L_Img, Hwindow);//顯示圖片
}
不拉伸,原圖比例顯示//圖片不拉伸顯示 public void ImgIsNotStretchDisplay(HObject L_Img, HTuple Hwindow) { HTuple hv_Width, hv_Height; HTuple win_Width, win_Height, win_Col, win_Row, cwin_Width, cwin_Height; HOperatorSet.ClearWindow(Hwindow); HOperatorSet.GetImageSize(L_Img, out hv_Width, out hv_Height);//獲取圖片大小規(guī)格 HOperatorSet.GetWindowExtents(Hwindow, out win_Row, out win_Col, out win_Width, out win_Height);//獲取窗體大小規(guī)格 cwin_Height = 1.0 * win_Height / win_Width * hv_Width;//寬不變計(jì)算高 if (cwin_Height > hv_Height)//寬不變高能容納 { cwin_Height = 1.0 * (cwin_Height - hv_Height) / 2; HOperatorSet.SetPart(Hwindow, -cwin_Height, 0, cwin_Height + hv_Height, hv_Width);//設(shè)置窗體的規(guī)格 } else//高不變寬能容納 { cwin_Width = 1.0 * win_Width / win_Height * hv_Height;//高不變計(jì)算寬 cwin_Width = 1.0 * (cwin_Width - hv_Width) / 2; HOperatorSet.SetPart(Hwindow, 0, -cwin_Width, hv_Height, cwin_Width + hv_Width);//設(shè)置窗體的規(guī)格 } HOperatorSet.DispObj(L_Img, Hwindow);//顯示圖片 }
圖像縮放//圖片縮小放大,配合鼠標(biāo)滾輪事件 public void ImgZoom(HObject L_Img, HTuple Hwindow, int Delta = 0) { HTuple Zoom, Row, Col, L_Button; HTuple hv_Width, hv_Height; HTuple Row0, Column0, Row00, Column00, Ht, Wt; HTuple[] Now_Pos = new HTuple[4]; if (Delta > 0)//鼠標(biāo)滾動(dòng)格值,一般120 { Zoom = 1.2;//向上滾動(dòng),放大倍數(shù) } else { Zoom = 0.8;//向下滾動(dòng),縮小倍數(shù) } HOperatorSet.GetMposition(Hwindow, out Row, out Col, out L_Button);//獲取當(dāng)前鼠標(biāo)的位置 HOperatorSet.GetPart(Hwindow, out Row0, out Column0, out Row00, out Column00);//獲取當(dāng)前窗體的大小規(guī)格 HOperatorSet.GetImageSize(L_Img, out hv_Width, out hv_Height);//獲取圖片大小規(guī)格 Ht = Row00 - Row0; Wt = Column00 - Column0; if (Ht * Wt < 32000 * 32000 || Zoom == 1.2) { Now_Pos[0] = (Row0 + ((1 - (1.0 / Zoom)) * (Row - Row0))); Now_Pos[1] = (Column0 + ((1 - (1.0 / Zoom)) * (Col - Column0))); Now_Pos[2] = Now_Pos[0] + (Ht / Zoom); Now_Pos[3] = Now_Pos[1] + (Wt / Zoom); HOperatorSet.SetPart(Hwindow, Now_Pos[0], Now_Pos[1], Now_Pos[2], Now_Pos[3]); HOperatorSet.ClearWindow(Hwindow); HOperatorSet.DispObj(L_Img, Hwindow); } else { ImgIsNotStretchDisplay(L_Img, Hwindow);//不拉伸顯示 }
}
圖像移動(dòng),實(shí)時(shí)移動(dòng):HTuple oldRow, oldColumn; //鼠標(biāo)按下去拖著圖像移動(dòng),配合鼠標(biāo)坐標(biāo)按下與移動(dòng)事件 public void MouseDownMoveImg(HObject L_Img, HTuple Hwindow) { HTuple row1, col1, row2, col2, Row, Column, Button; HOperatorSet.GetMposition(Hwindow, out Row, out Column, out Button); double RowMove = Row - oldRow; double ColMove = Column - oldColumn; HOperatorSet.GetPart(Hwindow, out row1, out col1, out row2, out col2);//得到當(dāng)前的窗口坐標(biāo) HOperatorSet.SetPart(Hwindow, row1 - RowMove, col1 - ColMove, row2 - RowMove, col2 - ColMove);
//防止刷新圖片太快的時(shí)候閃爍 HOperatorSet.SetSystem("flush_graphic", "false"); HOperatorSet.ClearWindow(Hwindow); HOperatorSet.SetSystem("flush_graphic", "true"); //
HOperatorSet.DispObj(L_Img, Hwindow); }
public void SaveMouseDownPosition(HTuple Hwindow) { HTuple Button; HOperatorSet.GetMposition(Hwindow, out oldRow, out oldColumn, out Button); }
圖像自適應(yīng)//設(shè)置自動(dòng)適應(yīng) private void tsMAutoFit_Click(object sender, EventArgs e) { try { ImgIsNotStretchDisplay(ho_Image1, hWindowControl1.HalconWindow); } catch (Exception) { MessageBox.Show("自適應(yīng)失敗,請(qǐng)插入圖像!"); } }
該文章在 2023/2/27 11:44:40 編輯過
|
|