其他分享
首页 > 其他分享> > 图像显示

图像显示

作者:互联网

HWindowControl控件图像显示:适应窗口,放大,缩放,平移

 1  public void ImageFitWindow(HImage hImage, HWindowControl hwindow)
 2         {
 3             HTuple width, height;
 4             hImage.GetImageSize(out width, out height);
 5             double ratioWidth = 1.0 * width.I / hwindow.WindowSize.Width;
 6             double ratioHeight = 1.0 * height / hwindow.WindowSize.Height;
 7 
 8             HTuple row1, column1, row2, column2;
 9 
10             if (ratioWidth > ratioHeight)
11             {
12                 row1 = -(1.0) * (hwindow.WindowSize.Height * ratioWidth - height.I) / 2;
13                 column1 = 0;
14                 row2 = row1 + hwindow.WindowSize.Height * ratioWidth;
15                 column2 = column1 + hwindow.WindowSize.Width * ratioWidth;
16             }
17             else
18             {
19                 row1 = 0;
20                 column1 = -(1.0) * (hwindow.WindowSize.Width * ratioHeight - height.I) / 2;
21                 row2 = row1 + hwindow.WindowSize.Height * ratioHeight;
22                 column2 = column1 + hwindow.WindowSize.Width * ratioHeight;
23 
24             }
25           //  hwindow.HalconWindow.SetPart(row1, column1, row2, column2);
26                HOperatorSet.SetPart(hwindow.HalconWindow, row1, column1, row2, column2);
27             hwindow.HalconWindow.DispObj(hImage);
28         }

借助鼠标事件实现

 1 public  class MouseEvent
 2     {
 3         private HTuple WindowID;
 4         private double RowDown;//鼠标按下时的行坐标
 5         private double ColDown;//鼠标按下时的列坐标
 6         private HObject hImage;
 7         HTuple ImageWidth, ImageHeight;
 8         public MouseEvent(HWindowControl hWindowControl,HObject hImage)
 9         {
10             this.WindowID = hWindowControl.HalconWindow;
11             if (hImage == null)
12                 return;
13             this.hImage = hImage;
14             HOperatorSet.GetImageSize(hImage,out ImageWidth,out ImageHeight);
15             hWindowControl.HMouseDown += new HMouseEventHandler(hWindowControl1_HMouseDown);
16             hWindowControl.HMouseUp += new HMouseEventHandler(hWindowControl1_HMouseUp);
17             hWindowControl.HMouseWheel += new HMouseEventHandler(hWindowControl1_HMouseWheel);
18             hWindowControl.HMouseMove += new HMouseEventHandler(hWindowControl1_HMouseMove);
19         }
20         //鼠标按下,记录当前坐标值
21         private void hWindowControl1_HMouseDown(object sender,HMouseEventArgs e)
22         {
23             HTuple Row, Column, Button;
24             HOperatorSet.GetMposition(WindowID, out Row,out Column, out Button);
25             RowDown = Row;    //鼠标按下时的行坐标
26             ColDown = Column; //鼠标按下时的列坐标
27         }
28         private void hWindowControl1_HMouseUp(object sender,HMouseEventArgs e)
29         {
30             HTuple row1, col1, row2, col2, Row, Column, Button;
31             HOperatorSet.GetMposition(WindowID, out Row, out Column, out Button);
32             double RowMove = Row - RowDown;   //鼠标弹起时的行坐标减去按下时的行坐标,得到行坐标的移动值
33             double ColMove = Column - ColDown;//鼠标弹起时的列坐标减去按下时的列坐标,得到列坐标的移动值
34             HOperatorSet.GetPart(WindowID, out row1, out col1, out row2, out col2);//得到当前的窗口坐标
35             HOperatorSet.SetPart(WindowID, row1 - RowMove, col1 - ColMove, row2 - RowMove, col2 - ColMove);//这里可能有些不好理解。以左上角原点为参考点
36             HOperatorSet.ClearWindow(WindowID);
37             HOperatorSet.DispObj(hImage, WindowID);
38         }
39         private void hWindowControl1_HMouseWheel(object sender, HMouseEventArgs e)
40         {
41 
42             HTuple Zoom, Row, Col, Button;
43             HTuple Row0, Column0, Row00, Column00, Ht, Wt, r1, c1, r2, c2;
44             if (e.Delta > 0)
45             {
46                 Zoom = 1.5;
47             }
48             else
49             {
50                 Zoom = 0.5;
51             }
52             HOperatorSet.GetMposition(WindowID, out Row, out Col, out Button);
53             HOperatorSet.GetPart(WindowID, out Row0, out Column0, out Row00, out Column00);
54             Ht = Row00 - Row0;
55             Wt = Column00 - Column0;
56             if (Ht * Wt < 32000 * 32000 || Zoom == 1.5)//普通版halcon能处理的图像最大尺寸是32K*32K。如果无限缩小原图像,导致显示的图像超出限制,则会造成程序崩溃
57             {
58                 r1 = (Row0 + ((1 - (1.0 / Zoom)) * (Row - Row0)));
59                 c1 = (Column0 + ((1 - (1.0 / Zoom)) * (Col - Column0)));
60                 r2 = r1 + (Ht / Zoom);
61                 c2 = c1 + (Wt / Zoom);
62                 HOperatorSet.SetPart(WindowID, r1, c1, r2, c2);
63                 HOperatorSet.ClearWindow(WindowID);
64                 HOperatorSet.DispObj(hImage, WindowID);
65             }
66         }
67         private void hWindowControl1_HMouseMove(object sender, HMouseEventArgs e)
68         {
69 
70             HTuple Row, Column, Button, pointGray;
71             HOperatorSet.GetMposition(WindowID, out Row, out Column, out Button);              //获取当前鼠标的坐标值
72             if (ImageHeight != null && (Row > 0 && Row < ImageHeight) && (Column > 0 && Column < ImageWidth))//设置3个条件项,防止程序崩溃。
73             {
74                 HOperatorSet.GetGrayval(hImage, Row, Column, out pointGray);                 //获取当前点的灰度值
75             }
76             else
77             {
78                 pointGray = "_";
79             }
80 
81         }
82     }

        这个文件更智能化,类似于康耐视图像显示窗口控件

        https://files.cnblogs.com/files/xingyuanzier/HWindow_Tool.zip?t=1649323448

标签:hImage,显示,HOperatorSet,hwindow,WindowID,图像,out,Row
来源: https://www.cnblogs.com/xingyuanzier/p/16113321.html