Halcon控件图像缩放

需要的变量定义

 private double RowDown;//鼠标按下时的行坐标
        private double ColDown;//鼠标按下时的列坐标
        HTuple Hmposition_Row, Hmposition_Col;
        HObject himg;
        HWindow hwin;
        bool bMouseDown = false;
        HTuple imgwidth, imgheight;

窗体加载事件读取本地图片,并适应Halcon控件大小

  private void Form1_Load(object sender, EventArgs e)
        {
            hwin = hWindowControl1.HalconWindow;
            HOperatorSet.ReadImage(out himg, AppDomain.CurrentDomain.BaseDirectory + "TP.bmp");
            HOperatorSet.GetImageSize(himg, out imgwidth, out imgheight);
            HOperatorSet.SetPart(hwin, 0, 0, imgheight, imgwidth);
            HOperatorSet.DispObj(himg, hwin);
        }

鼠标移动事件,实现图像的平移(这里和鼠标右键按下松开事件配合实现)

 private void hWindowControl1_HMouseMove(object sender, HMouseEventArgs e)
        {
            try
            {
                HTuple Hbutton_state, Hpixelgraval;

                HTuple row1, col1, row2, col2; //Row, Column, Button;

                HOperatorSet.GetMposition(hwin, out Hmposition_Row, out Hmposition_Col, out Hbutton_state);
                if (Hmposition_Col < 0)
                {
                    Hmposition_Col = 0;
                }
                if (Hmposition_Col > imgwidth - 1)
                {
                    Hmposition_Col = imgwidth - 1;
                }
                if (Hmposition_Row < 0)
                {
                    Hmposition_Row = 0;
                }
                if (Hmposition_Row > imgheight - 1)
                {
                    Hmposition_Row = imgheight - 1;
                }

                HOperatorSet.GetGrayval(himg, Hmposition_Row, Hmposition_Col, out Hpixelgraval);
                // toolStripStatusLabel7.Text = "(" + Hmposition_Row.ToString() + "," + Hmposition_Col.ToString() + ")=" + Hpixelgraval.ToString();
                if (bMouseDown)
                {
                    double RowMove = Hmposition_Row - RowDown;   //鼠标弹起时的行坐标减去按下时的行坐标,得到行坐标的移动值
                    double ColMove = Hmposition_Col - ColDown;//鼠标弹起时的列坐标减去按下时的列坐标,得到列坐标的移动值
                    HOperatorSet.GetPart(hwin, out row1, out col1, out row2, out col2);//得到当前的窗口坐标
                    HOperatorSet.SetPart(hwin, row1 - RowMove, col1 - ColMove, row2 - RowMove, col2 - ColMove);//这里可能有些不好理解。以左上角原点为参考点
                    HOperatorSet.ClearWindow(hwin);
                    if (imgheight != null)
                    {
                        HOperatorSet.SetSystem("flush_graphic", "true");
                        HOperatorSet.DispObj(himg, hwin);
                        HOperatorSet.SetSystem("flush_graphic", "false");
                    }
                    else
                    {
                        MessageBox.Show("请加载一张图片");
                    }
                }
            }
            catch { }
        }

鼠标滚轮事件,通过滚轮实现图像的放大缩小

private void hWindowControl1_HMouseWheel(object sender, HMouseEventArgs e)
        {
            try
            {
                HTuple Zoom, Row, Col, Button;
                HTuple Row0, Column0, Row00, Column00, Ht, Wt, r1, c1, r2, c2;
                if (e.Delta > 0)
                {
                    Zoom = 1.5;
                }
                else
                {
                    Zoom = 0.5;
                }
                HOperatorSet.GetMposition(hwin, out Row, out Col, out Button);
                HOperatorSet.GetPart(hwin, out Row0, out Column0, out Row00, out Column00);
                Ht = Row00 - Row0;
                Wt = Column00 - Column0;
                if (Ht * Wt < 32000 * 32000 || Zoom == 1.5)//普通版halcon能处理的图像最大尺寸是32K*32K。如果无限缩小原图像,导致显示的图像超出限制,则会造成程序崩溃
                {
                    r1 = (Row0 + ((1 - (1.0 / Zoom)) * (Row - Row0)));
                    c1 = (Column0 + ((1 - (1.0 / Zoom)) * (Col - Column0)));
                    r2 = r1 + (Ht / Zoom);
                    c2 = c1 + (Wt / Zoom);
                    HOperatorSet.SetPart(hwin, r1, c1, r2, c2);
                    HOperatorSet.ClearWindow(hwin);
                    HOperatorSet.SetSystem("flush_graphic", "true");
                    HOperatorSet.DispObj(himg, hwin);
                    HOperatorSet.SetSystem("flush_graphic", "false");
                }
            }
            catch { }
        }

鼠标松开事件

 private void hWindowControl1_HMouseUp(object sender, HMouseEventArgs e)
 {
            if (e.Button == MouseButtons.Right)
            {
                bMouseDown = false;
            }
 }

鼠标按下事件

private void hWindowControl1_HMouseDown(object sender, HMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                bMouseDown = true;
                HTuple Row, Column, Button;
                HOperatorSet.GetMposition(hwin, out Row, out Column, out Button);
                RowDown = Row;    //鼠标按下时的行坐标
                ColDown = Column; //鼠标按下时的列坐标
            }
        } 

缩放后通过按钮的点击事件恢复图像到适应Halcon控件的状态

private void button1_Click(object sender, EventArgs e)
       {
           HOperatorSet.GetImageSize(himg, out imgwidth, out imgheight);
           HOperatorSet.SetPart(hwin, 0, 0, imgheight - 1, imgheight - 1);
           hwin.DispObj(himg);
       }

界面:Halcon控件图像缩放

上一篇:android开发学习之Level List篇


下一篇:SpringBoot中Transaction注解不起作用原因