基本信息
源码名称:halcon与C#联合编程之鼠标控制图片缩放,拖动,roi
源码大小:13.32M
文件格式:.zip
开发语言:C#
更新时间:2017-12-01
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 5 元 
   源码介绍
本实例展示了halcon与C#联合编程之鼠标控制图片缩放,拖动,roi

using ChoiceTech.Halcon.Control;
using HalconDotNet;
using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace HalconPaint
{
    public partial class PaintForm : Form
    {
        private HObject image = new HObject();//图片
        private HObject brush_region = new HObject();//笔刷
        private HObject final_region = new HObject();//需要获得的区域
        private HTuple hv_WindowHandle;

        public PaintForm()
        {
            InitializeComponent();

            //hv_WindowHandle = hWindow_Final1.HWindowHalconID;
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void VisionForm_Load(object sender, EventArgs e)
        {

            try
            {
                HOperatorSet.ReadImage(out image, "kk.bmp");
                hWindow_Final1.HobjectToHimage(image);
            }
            catch (Exception)
            {
                throw;
            }
       
        }

        /// <summary>
        /// 设置笔刷
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_brush_Click(object sender, EventArgs e)
        {
            string brushType = ((Button)sender).Text;//笔刷类型

            HTuple hv_Row1 = null, hv_Column1 = null, hv_Row2 = null, hv_Column2 = null;

            HObject ho_temp_brush = new HObject();

            try
            {
                //画图模式 开
                hWindow_Final1.DrawModel = true;
                hWindow_Final1.Focus();
                //锁住功能区
                groupBox_tool.Enabled = false;
                //显示提示
                hWindow_Final1.ClearWindow();
                hWindow_Final1.HobjectToHimage(image);
                HalconTool.set_display_font(hv_WindowHandle, 20, "mono", new HTuple("true"), new HTuple("false"));
                HalconTool.disp_message(hv_WindowHandle, "在窗口画出" brushType ",点击右键结束", "window", 20, 20, "red", "false");

                //显示为黄色
                HOperatorSet.SetColor(hv_WindowHandle, "yellow");
                //
                switch (brushType)
                {
                    case "矩形1":

                        HOperatorSet.DrawRectangle1(hv_WindowHandle, out hv_Row1, out hv_Column1, out hv_Row2,
                           out hv_Column2);
                        ho_temp_brush.Dispose();
                        HOperatorSet.GenRectangle1(out ho_temp_brush, hv_Row1, hv_Column1, hv_Row2,
                            hv_Column2);
                        //
                        if (hv_Row1.D != 0)
                        {
                            brush_region.Dispose();
                            brush_region = ho_temp_brush;
                        }
                        else
                        {
                            hWindow_Final1.HobjectToHimage(image);
                            HalconTool.set_display_font(hv_WindowHandle, 20, "mono", new HTuple("true"), new HTuple("false"));
                            HalconTool.disp_message(hv_WindowHandle, "未画出有效区域", "window", 20, 20, "red", "false");
                            return;
                        }
                        break;
                    case "矩形2":
                        HTuple phi, lenght1, length2;
                        HOperatorSet.DrawRectangle2(hv_WindowHandle, out hv_Row1, out hv_Column1, out phi, out lenght1, out length2);

                        ho_temp_brush.Dispose();
                        HOperatorSet.GenRectangle2(out ho_temp_brush, hv_Row1, hv_Column1, phi, lenght1, length2);
                        //
                        if (hv_Row1.D != 0)
                        {
                            brush_region.Dispose();
                            brush_region = ho_temp_brush;
                        }
                        else
                        {
                            hWindow_Final1.HobjectToHimage(image);
                            HalconTool.set_display_font(hv_WindowHandle, 20, "mono", new HTuple("true"), new HTuple("false"));
                            HalconTool.disp_message(hv_WindowHandle, "未画出有效区域", "window", 20, 20, "red", "false");
                            return;
                        }
                        break;
                    case "圆":
                        HTuple radius;
                        HOperatorSet.DrawCircle(hv_WindowHandle, out hv_Row1, out hv_Column1, out radius);

                        ho_temp_brush.Dispose();
                        HOperatorSet.GenCircle(out ho_temp_brush, hv_Row1, hv_Column1, radius);
                        //
                        if (hv_Row1.D != 0)
                        {
                            brush_region.Dispose();
                            brush_region = ho_temp_brush;
                        }
                        else
                        {
                            hWindow_Final1.HobjectToHimage(image);
                            HalconTool.set_display_font(hv_WindowHandle, 20, "mono", new HTuple("true"), new HTuple("false"));
                            HalconTool.disp_message(hv_WindowHandle, "未画出有效区域", "window", 20, 20, "red", "false");
                            return;
                        }

                        break;
                    default:
                        MessageBox.Show("错误指令");
                        return;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                hWindow_Final1.HobjectToHimage(image);
                HalconTool.set_display_font(hv_WindowHandle, 20, "mono", new HTuple("true"), new HTuple("false"));
                HalconTool.disp_message(hv_WindowHandle, brushType " 笔刷创建成果", "window", 20, 20, "red", "false");
                hWindow_Final1.DispObj(ho_temp_brush, "yellow");
                hWindow_Final1.DrawModel = false;
                //解锁功能区
                groupBox_tool.Enabled = true;
            }

        }

        /// <summary>
        /// 擦除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_drawAndwipe_Click(object sender, EventArgs e)
        {
            string actionType = ((Button)sender).Name;// 画区域 或者 擦除
            hWindow_Final1.DrawModel = true;
            hWindow_Final1.Focus();
            groupBox_tool.Enabled = false;

            HTuple hv_Button = null;
            HTuple hv_Row = null, hv_Column = null;
            HTuple areaBrush, rowBrush, columnBrush, homMat2D;


            HObject brush_region_affine = new HObject();
            HObject ho_Image = new HObject(image);
            try
            {
                if (!brush_region.IsInitialized())
                {
                    MessageBox.Show("请先设置笔刷");
                    return;
                }
                else
                {
                    HOperatorSet.AreaCenter(brush_region, out areaBrush, out rowBrush, out columnBrush);
                }

                //显示
                hWindow_Final1.HobjectToHimage(image);
                hWindow_Final1.DispObj(final_region);

                //画出笔刷
                switch (actionType)
                {
                    case "button_draw":
                        HOperatorSet.SetColor(hv_WindowHandle, "blue");

                        break;
                    case "button_wipe":
                        HOperatorSet.SetColor(hv_WindowHandle, "red");

                        //检查final_region是否有效
                        if (!final_region.IsInitialized())
                        {
                            MessageBox.Show("请先使用画出合适区域,在使用擦除功能");
                            return;
                        }
                        break;
                    default:
                        MessageBox.Show("设置错误");
                        return;
                }

                #region "循环,等待涂抹"
 
                    //鼠标状态
                    hv_Button = 0;
                    // 4为鼠标右键
                    while (hv_Button != 4)
                    {
                        //一直在循环,需要让halcon控件也响应事件,不然到时候跳出循环,之前的事件会一起爆发触发,
                        Application.DoEvents();

                        hv_Row = -1;
                        hv_Column = -1;

                        //获取鼠标坐标
                        try
                        {
                            HOperatorSet.GetMposition(hv_WindowHandle, out hv_Row, out hv_Column, out hv_Button);
                        }
                        catch (HalconException ex)
                        {
                            hv_Button = 0;
                        }


                        HOperatorSet.SetSystem("flush_graphic", "false");
                        HOperatorSet.DispObj(ho_Image, hv_WindowHandle);
                        if (final_region.IsInitialized())
                        {
                            HOperatorSet.DispObj(final_region, hv_WindowHandle);
                        }


                        //check if mouse cursor is over window
                        if (hv_Row >= 0 && hv_Column >= 0)
                        {
                            //放射变换
                            HOperatorSet.VectorAngleToRigid(rowBrush, columnBrush, 0, hv_Row, hv_Column, 0, out  homMat2D);
                            brush_region_affine.Dispose();
                            HOperatorSet.AffineTransRegion(brush_region, out brush_region_affine, homMat2D, "nearest_neighbor");
                            HOperatorSet.DispObj(brush_region_affine, hv_WindowHandle);

                            HOperatorSet.SetSystem("flush_graphic", "true");
                            HalconTool.set_display_font(hv_WindowHandle, 20, "mono", new HTuple("true"), new HTuple("false"));
                            HalconTool.disp_message(hv_WindowHandle, "按下鼠标左键涂画,右键结束", "window", 20, 20, "red", "false");

                            //1为鼠标左键
                            if (hv_Button == 1)
                            {

                                //画出笔刷
                                switch (actionType)
                                {
                                    case "button_draw":
                                        {
                                            if (final_region.IsInitialized())
                                            {
                                                HObject ExpTmpOutVar_0;
                                                HOperatorSet.Union2(final_region, brush_region_affine, out ExpTmpOutVar_0);
                                                final_region.Dispose();
                                                final_region = ExpTmpOutVar_0;
                                            }
                                            else
                                            {
                                                final_region =new HObject( brush_region_affine);
                                            }

                                        }
                                        break;
                                    case "button_wipe":
                                        {
                                            HObject ExpTmpOutVar_0;
                                            HOperatorSet.Difference(final_region, brush_region_affine, out ExpTmpOutVar_0);
                                            final_region.Dispose();
                                            final_region = ExpTmpOutVar_0;
                                        }
                                        break;
                                    default:
                                        MessageBox.Show("设置错误");
                                        return;
                                }//end switch

                            }//end if
                        }
                        else
                        {
                            HalconTool.set_display_font(hv_WindowHandle, 20, "mono", new HTuple("true"), new HTuple("false"));
                            HalconTool.disp_message(hv_WindowHandle, "请将鼠标移动到窗口内部", "window", 20, 20, "red", "false");
                        }


                    }//end while
                #endregion
            }
            catch (HalconException HDevExpDefaultException)
            {
                throw HDevExpDefaultException;
            }
            finally
            {
                hWindow_Final1.HobjectToHimage(image);
                hWindow_Final1.DispObj(final_region, "blue");
                hWindow_Final1.DrawModel = false;
                groupBox_tool.Enabled = true;

            }
        }

        /// <summary>
        /// 显示结果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_show_finalRegion_Click(object sender, EventArgs e)
        {
            hWindow_Final1.HobjectToHimage(image);
            hWindow_Final1.DispObj(final_region,"blue");
        }


    }
}