Halcon学习之显示图像
-
当前使用软件版本:VS2017 + Halcon17.12
-
使用VS2017创建winform项目HalconDisplayImage。
-
通过nuget添加引用halcon动态库,注意选择版本17.12
-
选择form窗体,拖入HSmartWindowControl
-
进入cs文件添加命名空间using HalconDotNet;
-
编写读取图像函数
#region 获取图像 public static void GetImage(string image, out HObject hoImage) { HOperatorSet.GenEmptyObj(out hoImage); HOperatorSet.ReadImage(out hoImage, image); } #endregion
-
编写显示图像函数
#region 显示图像string public static void ShowImage(string image, HSmartWindowControl hWindowControl, bool isClearWindow = false) { hWindowControl.Invoke( new Action(() => { if (isClearWindow) { HOperatorSet.ClearWindow(hWindowControl.HalconID); } HTuple width = null; HTuple height = null; HObject hoImage; GetImage(image, out hoImage); HOperatorSet.GetImageSize(hoImage, out width, out height); bool needResizeImage = true; //获取图像大小及纵横比 HOperatorSet.GetImageSize(hoImage, out width, out height); int im_width = int.Parse(width.ToString()); int im_height = int.Parse(height.ToString()); double im_AspectRatio = (double)(im_width) / (double)(im_height); //获取窗口大小及纵横比 int w_width = hWindowControl.Size.Width; int w_height = hWindowControl.Size.Height; double w_AspectRatio = (double)(w_width) / (double)(w_height); HOperatorSet.SetSystem("int_zooming", "false");//图像缩放之前最好将此参数设置为false. HTuple para = new HTuple("constant"); HObject hoZoomImage; HOperatorSet.GenEmptyObj(out hoZoomImage); hoZoomImage.Dispose(); if (w_width < im_width && im_AspectRatio > w_AspectRatio) { //超宽图像 HOperatorSet.ZoomImageSize(hoImage, out hoZoomImage, w_width, w_width / im_AspectRatio, para); } else if (w_height < im_height && im_AspectRatio < w_AspectRatio) { //超高图像 HOperatorSet.ZoomImageSize(hoImage, out hoZoomImage, w_height * im_AspectRatio, w_height, para); } else { needResizeImage = false; } HOperatorSet.SetPart(hWindowControl.HalconID, 0, 0, -2, -2); if (needResizeImage) { HOperatorSet.DispObj(hoZoomImage, hWindowControl.HalconID); } else { HOperatorSet.DispObj(hoImage, hWindowControl.HalconID); } hoImage.Dispose(); hoZoomImage.Dispose(); })); } #endregion
-
更改项目属性,去掉“首选32位”
-
为窗体添加load事件
private void Form1_Load(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); //openFileDialog.InitialDirectory = "c:\\"; openFileDialog.Filter = "PNG|*.PNG|BMP|*.BMP|JPG|*.JPG|GIF|*.GIF"; openFileDialog.RestoreDirectory = true; openFileDialog.FilterIndex = 1; openFileDialog.Multiselect = false; if (openFileDialog.ShowDialog() == DialogResult.OK) { string fName = openFileDialog.FileName; ShowImage(fName, hSmartWindowControl1); } }
-
运行选择一张图片
-
添加鼠标滑轮放大缩小图片功能,在窗体的load事件的首行添加鼠标滑轮滚动事件,注意此处是针对窗体的MouseWheel 事件而不是hSmartWindowControl1控件
this.MouseWheel += hSmartWindowControl1_HMouseWheel;
private void hSmartWindowControl1_HMouseWheel(object sender, MouseEventArgs e) { System.Drawing.Point pt = this.Location; int leftBorder = hSmartWindowControl1.Location.X; int rightBorder = hSmartWindowControl1.Location.X + hSmartWindowControl1.Size.Width; int topBorder = hSmartWindowControl1.Location.Y; int bottomBorder = hSmartWindowControl1.Location.Y + hSmartWindowControl1.Size.Height; if (e.X > leftBorder && e.X < rightBorder && e.Y > topBorder && e.Y < bottomBorder) { MouseEventArgs newe = new MouseEventArgs(e.Button, e.Clicks, e.X - pt.X, e.Y - pt.Y, e.Delta); hSmartWindowControl1.HSmartWindowControl_MouseWheel(sender, newe); } }
-
滑动滑轮可以缩小图片
-
代码下载:https://download.****.net/download/weepingisgood/11122460