Halcon C# 设置窗体字体,自适应显示图片

设置窗体字体

// 使用
Form1.set_display_font(this.hWindowControl1, 24, "mono", "true", "false");
public static void set_display_font(HalconDotNet.HWindowControl hWindowCtl, HTuple hv_Size, HTuple hv_Font,
                                    HTuple hv_Bold, HTuple hv_Slant)
{

    HTuple hv_OS = null, hv_Fonts = new HTuple();
    HTuple hv_Style = null, hv_Exception = new HTuple(), hv_AvailableFonts = null;
    HTuple hv_Fdx = null, hv_Indices = new HTuple();
    HTuple hv_Font_COPY_INP_TMP = hv_Font.Clone();
    HTuple hv_Size_COPY_INP_TMP = hv_Size.Clone();

    HOperatorSet.GetSystem("operating_system", out hv_OS);
    // dev_get_preferences(...); only in hdevelop
    // dev_set_preferences(...); only in hdevelop
    if ((int)((new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(new HTuple()))).TupleOr(
        new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(-1)))) != 0)
    {
        hv_Size_COPY_INP_TMP = 16;
    }
    if ((int)(new HTuple(((hv_OS.TupleSubstr(0, 2))).TupleEqual("Win"))) != 0)
    {
        //Restore previous behaviour
        hv_Size_COPY_INP_TMP = ((1.13677 * hv_Size_COPY_INP_TMP)).TupleInt();
    }
    else
    {
        hv_Size_COPY_INP_TMP = hv_Size_COPY_INP_TMP.TupleInt();
    }
    if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("Courier"))) != 0)
    {
        hv_Fonts = new HTuple();
        hv_Fonts[0] = "Courier";
        hv_Fonts[1] = "Courier 10 Pitch";
        hv_Fonts[2] = "Courier New";
        hv_Fonts[3] = "CourierNew";
        hv_Fonts[4] = "Liberation Mono";
    }
    else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))) != 0)
    {
        hv_Fonts = new HTuple();
        hv_Fonts[0] = "Consolas";
        hv_Fonts[1] = "Menlo";
        hv_Fonts[2] = "Courier";
        hv_Fonts[3] = "Courier 10 Pitch";
        hv_Fonts[4] = "FreeMono";
        hv_Fonts[5] = "Liberation Mono";
    }
    else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0)
    {
        hv_Fonts = new HTuple();
        hv_Fonts[0] = "Luxi Sans";
        hv_Fonts[1] = "DejaVu Sans";
        hv_Fonts[2] = "FreeSans";
        hv_Fonts[3] = "Arial";
        hv_Fonts[4] = "Liberation Sans";
    }
    else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0)
    {
        hv_Fonts = new HTuple();
        hv_Fonts[0] = "Times New Roman";
        hv_Fonts[1] = "Luxi Serif";
        hv_Fonts[2] = "DejaVu Serif";
        hv_Fonts[3] = "FreeSerif";
        hv_Fonts[4] = "Utopia";
        hv_Fonts[5] = "Liberation Serif";
    }
    else
    {
        hv_Fonts = hv_Font_COPY_INP_TMP.Clone();
    }
    hv_Style = "";
    if ((int)(new HTuple(hv_Bold.TupleEqual("true"))) != 0)
    {
        hv_Style = hv_Style + "Bold";
    }
    else if ((int)(new HTuple(hv_Bold.TupleNotEqual("false"))) != 0)
    {
        hv_Exception = "Wrong value of control parameter Bold";
        throw new HalconException(hv_Exception);
    }
    if ((int)(new HTuple(hv_Slant.TupleEqual("true"))) != 0)
    {
        hv_Style = hv_Style + "Italic";
    }
    else if ((int)(new HTuple(hv_Slant.TupleNotEqual("false"))) != 0)
    {
        hv_Exception = "Wrong value of control parameter Slant";
        throw new HalconException(hv_Exception);
    }
    if ((int)(new HTuple(hv_Style.TupleEqual(""))) != 0)
    {
        hv_Style = "Normal";
    }
    hv_AvailableFonts = hWindowCtl.HalconWindow.QueryFont();
    //HOperatorSet.QueryFont(hv_WindowHandle, out hv_AvailableFonts);
    hv_Font_COPY_INP_TMP = "";
    for (hv_Fdx = 0; (int)hv_Fdx <= (int)((new HTuple(hv_Fonts.TupleLength())) - 1); hv_Fdx = (int)hv_Fdx + 1)
    {
        hv_Indices = hv_AvailableFonts.TupleFind(hv_Fonts.TupleSelect(hv_Fdx));
        if ((int)(new HTuple((new HTuple(hv_Indices.TupleLength())).TupleGreater(0))) != 0)
        {
            if ((int)(new HTuple(((hv_Indices.TupleSelect(0))).TupleGreaterEqual(0))) != 0)
            {
                hv_Font_COPY_INP_TMP = hv_Fonts.TupleSelect(hv_Fdx);
                break;
            }
        }
    }
    if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual(""))) != 0)
    {
        throw new HalconException("Wrong value of control parameter Font");
    }
    hv_Font_COPY_INP_TMP = (((hv_Font_COPY_INP_TMP + "-") + hv_Style) + "-") + hv_Size_COPY_INP_TMP;

    hWindowCtl.HalconWindow.SetFont(hv_Font_COPY_INP_TMP);
    

    return;
}

自适应显示图片

// 使用显示
Form1.DisplayImage(this.hWindowControl1, imgSub);
// 显示图片
private static void DisplayImage(HalconDotNet.HWindowControl hWindowCtl, HImage imgShow)
{
    // 设置显示大小
    imgShow.GetImageSize(out int imgWidth, out int imgHeight);

    double ratioWidth = (1.0) * imgWidth / hWindowCtl.Width;
    double ratioHeight = (1.0) * imgHeight / hWindowCtl.Height;

    HTuple row1, column1, row2, column2;
    if (ratioWidth >= ratioHeight)
    {
        row1 = -(1.0) * ((hWindowCtl.Height * ratioWidth) - imgHeight) / 2;
        column1 = 0;
        row2 = row1 + hWindowCtl.Height * ratioWidth;
        column2 = column1 + hWindowCtl.Width * ratioWidth;
    }
    else
    {
        row1 = 0;
        column1 = -(1.0) * ((hWindowCtl.Width * ratioHeight) - imgWidth) / 2;
        row2 = row1 + hWindowCtl.Height * ratioHeight;
        column2 = column1 + hWindowCtl.Width * ratioHeight;
    }

    // 显示图片
    hWindowCtl.HalconWindow.SetPart(row1, column1, row2, column2);
    
    hWindowCtl.HalconWindow.DispObj(imgShow);
}

注意dll需要放在一个文件夹中

halcon.dll
hcanvas.dll
halcondotnet.dll
在这里插入图片描述

  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C# 窗体中使用 Halcon 的话,你可以使用 Halcon 的 HWindow 控件来显示图片。以下是一个简单的示例代码: ```csharp using HalconDotNet; using System; using System.Windows.Forms; namespace HalconFormDemo { public partial class MainForm : Form { private HWindowControl hWindowControl; public MainForm() { InitializeComponent(); // 创建 HWindowControl 控件 hWindowControl = new HWindowControl(); hWindowControl.Dock = DockStyle.Fill; this.Controls.Add(hWindowControl); } private void MainForm_Load(object sender, EventArgs e) { // 设置 Halcon 库的安装路径 string halconPath = "C:\\Program Files\\MVTec\\HALCON-18.11-Progress\\bin\\x64-win64"; Environment.SetEnvironmentVariable("HALCONROOT", halconPath); // 初始化 Halcon HOperatorSet.SetSystem("width", 800); HOperatorSet.SetSystem("height", 600); } private void btnLoadImage_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Image Files|*.bmp;*.jpg;*.jpeg;*.png;*.tif;*.tiff"; if (openFileDialog.ShowDialog() == DialogResult.OK) { // 加载图像 HObject image; HOperatorSet.ReadImage(out image, openFileDialog.FileName); // 在 HWindowControl 控件中显示图像 hWindowControl.HalconWindow.DispObj(image); // 释放图像资源 image.Dispose(); } } } } ``` 在上面的示例中,我们使用了 HWindowControl 控件来显示图像。在窗体的 `btnLoadImage_Click` 事件处理程序中,我们通过 OpenFileDialog 选择图像文件,然后使用 HOperatorSet.ReadImage 方法读取图像,并使用 HWindowControl.HalconWindow.DispObj 方法在窗体显示图像。 需要注意的是,在加载图像之前,我们需要设置 Halcon 库的安装路径,并初始化 Halcon 系统参数。 希望这个示例能帮助到你!如果还有其他问题,请随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

廷益--飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值