win7系統vs2017實現halcon18.11與C#聯合編程

1、halcon代碼

dev_close_window()
read_image (Image, 'E:/桌面圖片/351218-103.jpg')
get_image_size(Image, Width, Height)
ScaleImage := 0.3
*默認dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_open_window (0, 0, Width * ScaleImage, Height * ScaleImage, 'black', WindowHandle)
*顯示圖像
dev_display (Image)

2、導出爲c#代碼

導出的代碼爲下面圖片所示,後面再對其做一定的修改

using HalconDotNet;

public partial class HDevelopExport
{
#if !(NO_EXPORT_MAIN || NO_EXPORT_APP_MAIN)
  public HDevelopExport()
  {
    // Default settings used in HDevelop
    HOperatorSet.SetSystem("width", 512);
    HOperatorSet.SetSystem("height", 512);
    if (HalconAPI.isWindows)
      HOperatorSet.SetSystem("use_window_thread","true");
    action();
  }
#endif

#if !NO_EXPORT_MAIN
  // Main procedure 
  private void action()
  {


    // Local iconic variables 

    HObject ho_Image;

    // Local control variables 

    HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
    HTuple hv_ScaleImage = new HTuple(), hv_WindowHandle = new HTuple();
    // Initialize local and output iconic variables 
    HOperatorSet.GenEmptyObj(out ho_Image);
    if (HDevWindowStack.IsOpen())
    {
      HOperatorSet.CloseWindow(HDevWindowStack.Pop());
    }
    ho_Image.Dispose();
    HOperatorSet.ReadImage(out ho_Image, "E:/桌面圖片/351218-103.jpg");
    hv_Width.Dispose();hv_Height.Dispose();
    HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
    hv_ScaleImage.Dispose();
    hv_ScaleImage = 0.3;
    //dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
    HOperatorSet.SetWindowAttr("background_color","black");
    HOperatorSet.OpenWindow(0,0,hv_Width*hv_ScaleImage,hv_Height*hv_ScaleImage,0,"visible","",out hv_WindowHandle);
    HDevWindowStack.Push(hv_WindowHandle);
    //
    if (HDevWindowStack.IsOpen())
    {
      HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
    }
    ho_Image.Dispose();

    hv_Width.Dispose();
    hv_Height.Dispose();
    hv_ScaleImage.Dispose();
    hv_WindowHandle.Dispose();

  }

#endif


}
#if !(NO_EXPORT_MAIN || NO_EXPORT_APP_MAIN)
public class HDevelopExportApp
{
  static void Main(string[] args)
  {
    new HDevelopExport();
  }
}
#endif

3、使用vs2017新建一個c#工程

4、添加引用

5、添加halcon窗口控件:打開Form1.cs界面並選中,打開工具欄,右鍵添加選項卡,得到HwindowControl控件。

6、添加一個按鈕和窗口

7、爲了使用添加的窗口顯示,並自適應圖像的大小,我們修改代碼

unnamed.cs代碼:

//
// File generated by HDevelop for HALCON/.NET (C#) Version 18.11.1.0
// Non-ASCII strings in this file are encoded in local-8-bit encoding (cp936).
// 
// Please note that non-ASCII characters in string constants are exported
// as octal codes in order to guarantee that the strings are correctly
// created on all systems, independent on any compiler settings.
// 
// Source files with different encoding should not be mixed in one project.
//

using HalconDotNet;

public partial class HDevelopExport
{
    public HTuple hv_ExpDefaultWinHandle;
    public HDevelopExport(HTuple Window)
  {      
    // Default settings used in HDevelop
    HOperatorSet.SetSystem("width", 512);
    HOperatorSet.SetSystem("height", 512);
    if (HalconAPI.isWindows)
      HOperatorSet.SetSystem("use_window_thread","true");
    action(Window);
  }

  // Main procedure 
  private void action(HTuple Window)
  {
    hv_ExpDefaultWinHandle = Window;
    HObject ho_Image;

    HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
    HTuple hv_ScaleImage = new HTuple();
    HTuple hv_WindowHandle = new HTuple();

    HOperatorSet.GenEmptyObj(out ho_Image);
    ho_Image.Dispose();
    HOperatorSet.ReadImage(out ho_Image, "E:/桌面圖片/351218-103.jpg");

    hv_Width.Dispose();hv_Height.Dispose();
    HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
        
    hv_ScaleImage.Dispose();
    hv_ScaleImage = 0.3;
    //默認dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
    HOperatorSet.SetWindowAttr("background_color","black");
    HOperatorSet.OpenWindow(0,0,hv_Width*hv_ScaleImage,hv_Height*hv_ScaleImage,0,"visible","",out hv_WindowHandle);
    HDevWindowStack.Push(hv_WindowHandle);

    //將讀取的模板圖像顯示在圖像窗口中
    HOperatorSet.ClearWindow(hv_ExpDefaultWinHandle);

    //和圖像大小一致(1024, 1280)
    HOperatorSet.SetPart(hv_ExpDefaultWinHandle, 0, 0, 1024, 1280);
    HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
    
    //顯示圖像
    if (HDevWindowStack.IsOpen())
    {
       HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
    }
    ho_Image.Dispose();

    hv_Width.Dispose();
    hv_Height.Dispose();
    hv_ScaleImage.Dispose();
    hv_WindowHandle.Dispose();

  }
}

 Form1.cs代碼:

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;

namespace example
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void hWindowControl1_HMouseMove(object sender, HalconDotNet.HMouseEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            HDevelopExport HDevelop = new HDevelopExport(hWindowControl1.HalconWindow); //hWindowControl1.HalconWindow爲創建的halcon窗口句柄。
        }
    }
}

 8、結果:

9、參考博客:

https://blog.csdn.net/bikede/article/details/81588337

https://blog.csdn.net/ruotianxia/article/details/81638552

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章