1. 程式人生 > >C# halcon 讀取圖片後自適應視窗demo

C# halcon 讀取圖片後自適應視窗demo

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;
namespace halconTest
{ 
    public partial class Form1 : Form
    {
        private HObject m_ho_Image;
        private HTuple m_hv_HWindowID;
        public Form1()
        {
            InitializeComponent();
            HOperatorSet.GenEmptyObj(out m_ho_Image);
            m_hv_HWindowID = new HTuple();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            m_hv_HWindowID = hWindowControl1.HalconID;
        }

        private void button1_Click(object sender, EventArgs e)
        {


            try
            {
                HOperatorSet.ReadImage(out m_ho_Image, "C:/Users/Public/Pictures/Sample Pictures/Koala.jpg");

                HTuple width, height;
                HOperatorSet.GetImageSize(m_ho_Image, out width, out height);

                double ratioWidth = (1.0) * width[0].I / hWindowControl1.Width;
                double ratioHeight = (1.0) * height[0].I / hWindowControl1.Height;


                HTuple row1, column1, row2, column2;
                if (ratioWidth >= ratioHeight)
                {
                    row1 = -(1.0) * ((hWindowControl1.Height * ratioWidth) - height) / 2;
                    column1 = 0;
                    //錯了
                    row2 = row1 + hWindowControl1.Height * ratioWidth;
                    column2 = column1 + hWindowControl1.Width * ratioWidth;
                    HOperatorSet.SetPart(m_hv_HWindowID, row1, column1, row2, column2);
                }
                else
                {

                }

                HOperatorSet.DispObj(m_ho_Image, m_hv_HWindowID);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
              
        }
    }

}