读取图片
//获取文件路径下的图片
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);//不拉伸显示
}
}
图像移动,实时移动
解决移动太快图像刷新闪烁问题文章来源:https://www.toymoban.com/news/detail-634269.html
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);
}
图像自适应
这里我们采用不拉伸显示函数文章来源地址https://www.toymoban.com/news/detail-634269.html
//设置自动适应
private void tsMAutoFit_Click(object sender, EventArgs e)
{
try
{
ImgIsNotStretchDisplay(ho_Image1, hWindowControl1.HalconWindow);
}
catch (Exception)
{
MessageBox.Show("自适应失败,请插入图像!");
}
}
到了这里,关于C#与halcon联合 缩放移动自适应图像的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!