一、简介
如何用HSmartWindowControl 控件在vs 上面显示一张图片。
1、加载HSmartWindowControl控件
2、在Form的构造函数中添加窗口和对鼠标混轮的事件进行注册
public Form1()
{
InitializeComponent();
hwindow = hSmartWindowControl1.HalconWindow;//从初始化窗口变量
// 鼠标滚轮的响应函数注册
this.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.my_MouseWheel);
}
3、实现2中已经注册的鼠标滚轮函数my_MouseWheel
a、获得以屏幕坐标表示的from 左上角的Point
System.Drawing.Point pt = this.Location;//
b、计算在HSmartWindowControl中对于的位置
MouseEventArgs newe = new MouseEventArgs(e.Button, e.Clicks, e.X - pt.X, e.Y - pt.Y, e.Delta);
hSmartWindowControl1.HSmartWindowControl_MouseWheel(sender, newe);
4、显示:
二、代码显示
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;
namespace HSmartWindowControl控件显示
{
public partial class Form1 : Form
{
private static HWindow hwindow;
private string picturePath;
public Form1()
{
InitializeComponent();
hwindow = hSmartWindowControl1.HalconWindow;//从初始化窗口变量
// 鼠标滚轮的响应函数注册
this.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.my_MouseWheel);
}
/// <summary>
/// 放大和缩小
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void my_MouseWheel(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);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
// picturePath= GetPicturePath();
#region 初始化变量
// Local iconic variables
HObject ho_Image;
// Local control variables
HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
// Initialize local and output iconic variables
HOperatorSet.GenEmptyObj(out ho_Image);
ho_Image.Dispose();
// 加载图片
HOperatorSet.ReadImage(out ho_Image, "C:\\Users\\alber\\Desktop\\tsst\\aa.jpg");
hv_Width.Dispose(); hv_Height.Dispose();
#endregion
#region 缩放图像,适应窗口
bool needResizeImage = true;
//获取图像大小及纵横比
HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
int im_width = int.Parse(hv_Width.ToString());
int im_height = int.Parse(hv_Height.ToString());
double im_AspectRatio = (double)(im_width) / (double)(im_height);
//获取窗口大小及纵横比
int w_width = hSmartWindowControl1.Size.Width;
int w_height = hSmartWindowControl1.Size.Height;
double w_AspectRatio = (double)(w_width) / (double)(w_height);
HOperatorSet.SetSystem("int_zooming", "false");//图像缩放之前最好将此参数设置为false.
HTuple para = new HTuple("constant");
HObject ho_zoomImage;
HOperatorSet.GenEmptyObj(out ho_zoomImage);
ho_zoomImage.Dispose();
if (w_width < im_width && im_AspectRatio > w_AspectRatio)
{
//超宽图像
HOperatorSet.ZoomImageSize(ho_Image, out ho_zoomImage, w_width, w_width / im_AspectRatio, para);
}
else if (w_height < im_height && im_AspectRatio < w_AspectRatio)
{
//超高图像
HOperatorSet.ZoomImageSize(ho_Image, out ho_zoomImage, w_height * im_AspectRatio, w_height, para);
}
else
{
needResizeImage = false;
}
#endregion
#region 显示图像
hwindow.SetPart(0, 0, -2, -2);
if (needResizeImage)
{
hwindow.DispObj(ho_zoomImage);
}
else
{
hwindow.DispObj(ho_Image);
}
#endregion
ho_Image.Dispose();
ho_zoomImage.Dispose();
hv_Width.Dispose();
hv_Height.Dispose();
}
private string GetPicturePath()
{
string path = "";
try
{
FolderBrowserDialog openFloder = new FolderBrowserDialog();
if( openFloder.ShowDialog() != DialogResult.OK)
{
return path;
}
openFloder.Description = "请选择图片的路径";
// openFloder.SelectedPath是选择的文件夹
DirectoryInfo directoryInfo = new DirectoryInfo(openFloder.SelectedPath);
DirectoryInfo[] arrayPicture=directoryInfo.GetDirectories();
path = openFloder.SelectedPath + "\\" + "*.png";
if (arrayPicture.Length==0)
{
path = "clip.png";
}
}
catch(Exception ex)
{
}
return path;
}
}
}