HSmartWindowControl 之显示图像

一、简介

如何用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;
        }
    }
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实时显示摄像头视频可以使用WPF中的MediaElement控件来实现。具体步骤如下: 1.在XAML文件中添加MediaElement控件,并设置其Source属性为摄像头视频的地址: ```xml <MediaElement x:Name="videoPlayer" Source="摄像头视频地址" /> ``` 2.在代码中使用DirectShowLib库来获取摄像头视频流,并将其绑定到MediaElement控件上: ```csharp using DirectShowLib; private void StartCamera() { // 获取摄像头设备 DsDevice[] devices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice); // 初始化视频捕获设备 IFilterGraph2 filterGraph = new FilterGraph() as IFilterGraph2; ICaptureGraphBuilder2 captureGraphBuilder = new CaptureGraphBuilder2() as ICaptureGraphBuilder2; captureGraphBuilder.SetFiltergraph(filterGraph); IBaseFilter sourceFilter = null; captureGraphBuilder.FindInterface(PinCategory.Capture, MediaType.Video, devices[0], typeof(IBaseFilter).GUID, out sourceFilter); // 添加视频捕获设备到FilterGraph中 IBaseFilter nullRenderer = new NullRenderer() as IBaseFilter; filterGraph.AddFilter(sourceFilter, "source"); filterGraph.AddFilter(nullRenderer, "null"); // 获取视频捕获设备的输出端口 IPin sourceOutputPin = DsFindPin.ByDirection(sourceFilter, PinDirection.Output, 0); IPin nullInputPin = DsFindPin.ByDirection(nullRenderer, PinDirection.Input, 0); // 连接视频捕获设备和nullRenderer filterGraph.Connect(sourceOutputPin, nullInputPin); // 将视频流绑定到MediaElement控件 IMediaControl mediaControl = filterGraph as IMediaControl; mediaControl.Run(); videoPlayer.Source = MediaCoreFactory.CreateMediaStreamSource(new MediaUriSource(new Uri("http://localhost:8080"))); } ``` 需要注意的是,如果摄像头设备有多个,需要根据实际情况选择需要使用的设备。 3.启动应用程序后,摄像头视频就会实时显示在MediaElement控件中了。 以上就是使用WPF中的MediaElement控件来实现实时显示摄像头视频的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值