C#与halcon联合编程之窗体内图像常见操作
|
admin
2023年2月27日 11:44
本文热度 1044
|
目录:读取图片
拉伸显示
不拉伸,原图比例显示
图像缩放
图像移动,实时移动
图像自适应
读取图片://获取文件路径下的图片 public HObject GetImgfromPath(string imgPath) { HObject L_Img; HOperatorSet.GenEmptyObj(out L_Img);//清空图片 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);//获取图片大小规格 HOperatorSet.SetPart(Hwindow, 0, 0, hv_Height, hv_Width);//设置窗体的规格 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);//获取图片大小规格 HOperatorSet.GetWindowExtents(Hwindow, out win_Row, out win_Col, out win_Width, out win_Height);//获取窗体大小规格 cwin_Height = 1.0 * win_Height / win_Width * hv_Width;//宽不变计算高 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);//设置窗体的规格 } else//高不变宽能容纳 { cwin_Width = 1.0 * win_Width / win_Height * hv_Height;//高不变计算宽 cwin_Width = 1.0 * (cwin_Width - hv_Width) / 2; HOperatorSet.SetPart(Hwindow, 0, -cwin_Width, hv_Height, cwin_Width + hv_Width);//设置窗体的规格 } HOperatorSet.DispObj(L_Img, Hwindow);//显示图片 }
图像缩放//图片缩小放大,配合鼠标滚轮事件 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)//鼠标滚动格值,一般120 { Zoom = 1.2;//向上滚动,放大倍数 } else { Zoom = 0.8;//向下滚动,缩小倍数 } HOperatorSet.GetMposition(Hwindow, out Row, out Col, out L_Button);//获取当前鼠标的位置 HOperatorSet.GetPart(Hwindow, out Row0, out Column0, out Row00, out Column00);//获取当前窗体的大小规格 HOperatorSet.GetImageSize(L_Img, out hv_Width, out hv_Height);//获取图片大小规格 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);//不拉伸显示 }
}
图像移动,实时移动:HTuple oldRow, oldColumn; //鼠标按下去拖着图像移动,配合鼠标坐标按下与移动事件 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);//得到当前的窗口坐标 HOperatorSet.SetPart(Hwindow, row1 - RowMove, col1 - ColMove, row2 - RowMove, col2 - ColMove);
//防止刷新图片太快的时候闪烁 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); }
图像自适应//设置自动适应 private void tsMAutoFit_Click(object sender, EventArgs e) { try { ImgIsNotStretchDisplay(ho_Image1, hWindowControl1.HalconWindow); } catch (Exception) { MessageBox.Show("自适应失败,请插入图像!"); } }
该文章在 2023/2/27 11:44:40 编辑过
|
|