读取图片

//获取文件路径下的图片
        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("自适应失败,请插入图像!");
            }          
        }

原文地址:https://blog.csdn.net/Bamboo265925/article/details/132166637

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_14991.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注