Winform控件

该文章展示了如何在C#的Winform应用程序中利用Halcon库在PictureBox控件中显示图像。代码示例包括读取图像、设置窗口大小、显示图像以及处理多个图像的点击事件。此外,还涉及到了在图形窗口中显示文本的辅助函数。
摘要由CSDN通过智能技术生成


工具箱

在这里插入图片描述

button

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

单选框

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

多选框

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

文本框

在这里插入图片描述

在这里插入图片描述

标签

显示提示信息
在这里插入图片描述

在这里插入图片描述

显示图片控件

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
最后的效果
在这里插入图片描述

进度条属性

在这里插入图片描述

在这里插入图片描述

配合button使用的步骤如下:
在这里插入图片描述

在这里插入图片描述

Halcon图片在PictureBox控件中显示

添加Halcon的引用

见我的这篇博文

编写的代码如下

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 WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        HTuple window = new HTuple();
        public Form1()
        {
            InitializeComponent();
            HOperatorSet.OpenWindow(0, 0, pictureBox1.Width, pictureBox1.Height, pictureBox1.Handle, "", "", out window);
            HDevWindowStack.Push(window);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            HOperatorSet.ReadImage(out HObject image, "F://halcon//halconStudy//相机标定//1.png");
            HOperatorSet.GetImageSize(image, out HTuple width, out HTuple heigth);
            HOperatorSet.SetPart(window, 0, 0, heigth - 1, width - 1);
            HOperatorSet.DispObj(image, window);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            HOperatorSet.ReadImage(out HObject image, "F://halcon//halconStudy//相机标定//2.png");
            HOperatorSet.GetImageSize(image, out HTuple width, out HTuple heigth);
            HOperatorSet.SetPart(window, 0, 0, heigth - 1, width - 1);
            HOperatorSet.DispObj(image, window);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            HOperatorSet.ReadImage(out HObject image, "F://halcon//halconStudy//相机标定//3.png");
            HOperatorSet.GetImageSize(image, out HTuple width, out HTuple heigth);
            HOperatorSet.SetPart(window, 0, 0, heigth - 1, width - 1);
            HOperatorSet.DispObj(image, window);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            HOperatorSet.ReadImage(out HObject image, "F://halcon//halconStudy//相机标定//4.png");
            HOperatorSet.GetImageSize(image, out HTuple width, out HTuple heigth);
            HOperatorSet.SetPart(window, 0, 0, heigth - 1, width - 1);
            HOperatorSet.DispObj(image, window);
        }
    }
}

在这里插入图片描述

在PictureBox中显示图像变量HObject

先将之前的代码导入

这个是Halcon的导出代码…
在这里插入图片描述

在这里插入图片描述
复制到C#项目中
在这里插入图片描述
完整代码:

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 WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        HTuple window = new HTuple();
        public void disp_message(HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem,
    HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box)
        {



            // Local iconic variables 

            // Local control variables 

            HTuple hv_Red = null, hv_Green = null, hv_Blue = null;
            HTuple hv_Row1Part = null, hv_Column1Part = null, hv_Row2Part = null;
            HTuple hv_Column2Part = null, hv_RowWin = null, hv_ColumnWin = null;
            HTuple hv_WidthWin = null, hv_HeightWin = null, hv_MaxAscent = null;
            HTuple hv_MaxDescent = null, hv_MaxWidth = null, hv_MaxHeight = null;
            HTuple hv_R1 = new HTuple(), hv_C1 = new HTuple(), hv_FactorRow = new HTuple();
            HTuple hv_FactorColumn = new HTuple(), hv_UseShadow = null;
            HTuple hv_ShadowColor = null, hv_Exception = new HTuple();
            HTuple hv_Width = new HTuple(), hv_Index = new HTuple();
            HTuple hv_Ascent = new HTuple(), hv_Descent = new HTuple();
            HTuple hv_W = new HTuple(), hv_H = new HTuple(), hv_FrameHeight = new HTuple();
            HTuple hv_FrameWidth = new HTuple(), hv_R2 = new HTuple();
            HTuple hv_C2 = new HTuple(), hv_DrawMode = new HTuple();
            HTuple hv_CurrentColor = new HTuple();
            HTuple hv_Box_COPY_INP_TMP = hv_Box.Clone();
            HTuple hv_Color_COPY_INP_TMP = hv_Color.Clone();
            HTuple hv_Column_COPY_INP_TMP = hv_Column.Clone();
            HTuple hv_Row_COPY_INP_TMP = hv_Row.Clone();
            HTuple hv_String_COPY_INP_TMP = hv_String.Clone();

            // Initialize local and output iconic variables 
            //This procedure displays text in a graphics window.
            //
            //Input parameters:
            //WindowHandle: The WindowHandle of the graphics window, where
            //   the message should be displayed
            //String: A tuple of strings containing the text message to be displayed
            //CoordSystem: If set to 'window', the text position is given
            //   with respect to the window coordinate system.
            //   If set to 'image', image coordinates are used.
            //   (This may be useful in zoomed images.)
            //Row: The row coordinate of the desired text position
            //   If set to -1, a default value of 12 is used.
            //Column: The column coordinate of the desired text position
            //   If set to -1, a default value of 12 is used.
            //Color: defines the color of the text as string.
            //   If set to [], '' or 'auto' the currently set color is used.
            //   If a tuple of strings is passed, the colors are used cyclically
            //   for each new textline.
            //Box: If Box[0] is set to 'true', the text is written within an orange box.
            //     If set to' false', no box is displayed.
            //     If set to a color string (e.g. 'white', '#FF00CC', etc.),
            //       the text is written in a box of that color.
            //     An optional second value for Box (Box[1]) controls if a shadow is displayed:
            //       'true' -> display a shadow in a default color
            //       'false' -> display no shadow (same as if no second value is given)
            //       otherwise -> use given string as color string for the shadow color
            //
            //Prepare window
            HOperatorSet.GetRgb(hv_WindowHandle, out hv_Red, out hv_Green, out hv_Blue);
            HOperatorSet.GetPart(hv_WindowHandle, out hv_Row1Part, out hv_Column1Part, out hv_Row2Part,
                out hv_Column2Part);
            HOperatorSet.GetWindowExtents(hv_WindowHandle, out hv_RowWin, out hv_ColumnWin,
                out hv_WidthWin, out hv_HeightWin);
            HOperatorSet.SetPart(hv_WindowHandle, 0, 0, hv_HeightWin - 1, hv_WidthWin - 1);
            //
            //default settings
            if ((int)(new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(-1))) != 0)
            {
                hv_Row_COPY_INP_TMP = 12;
            }
            if ((int)(new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(-1))) != 0)
            {
                hv_Column_COPY_INP_TMP = 12;
            }
            if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0)
            {
                hv_Color_COPY_INP_TMP = "";
            }
            //
            hv_String_COPY_INP_TMP = ((("" + hv_String_COPY_INP_TMP) + "")).TupleSplit("\n");
            //
            //Estimate extentions of text depending on font size.
            HOperatorSet.GetFontExtents(hv_WindowHandle, out hv_MaxAscent, out hv_MaxDescent,
                out hv_MaxWidth, out hv_MaxHeight);
            if ((int)(new HTuple(hv_CoordSystem.TupleEqual("window"))) != 0)
            {
                hv_R1 = hv_Row_COPY_INP_TMP.Clone();
                hv_C1 = hv_Column_COPY_INP_TMP.Clone();
            }
            else
            {
                //Transform image to window coordinates
                hv_FactorRow = (1.0 * hv_HeightWin) / ((hv_Row2Part - hv_Row1Part) + 1);
                hv_FactorColumn = (1.0 * hv_WidthWin) / ((hv_Column2Part - hv_Column1Part) + 1);
                hv_R1 = ((hv_Row_COPY_INP_TMP - hv_Row1Part) + 0.5) * hv_FactorRow;
                hv_C1 = ((hv_Column_COPY_INP_TMP - hv_Column1Part) + 0.5) * hv_FactorColumn;
            }
            //
            //Display text box depending on text size
            hv_UseShadow = 1;
            hv_ShadowColor = "gray";
            if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleEqual("true"))) != 0)
            {
                if (hv_Box_COPY_INP_TMP == null)
                    hv_Box_COPY_INP_TMP = new HTuple();
                hv_Box_COPY_INP_TMP[0] = "#fce9d4";
                hv_ShadowColor = "#f28d26";
            }
            if ((int)(new HTuple((new HTuple(hv_Box_COPY_INP_TMP.TupleLength())).TupleGreater(
                1))) != 0)
            {
                if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual("true"))) != 0)
                {
                    //Use default ShadowColor set above
                }
                else if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual(
                    "false"))) != 0)
                {
                    hv_UseShadow = 0;
                }
                else
                {
                    hv_ShadowColor = hv_Box_COPY_INP_TMP[1];
                    //Valid color?
                    try
                    {
                        HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect(
                            1));
                    }
                    // catch (Exception) 
                    catch (HalconException HDevExpDefaultException1)
                    {
                        HDevExpDefaultException1.ToHTuple(out hv_Exception);
                        hv_Exception = "Wrong value of control parameter Box[1] (must be a 'true', 'false', or a valid color string)";
                        throw new HalconException(hv_Exception);
                    }
                }
            }
            if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleNotEqual("false"))) != 0)
            {
                //Valid color?
                try
                {
                    HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect(0));
                }
                // catch (Exception) 
                catch (HalconException HDevExpDefaultException1)
                {
                    HDevExpDefaultException1.ToHTuple(out hv_Exception);
                    hv_Exception = "Wrong value of control parameter Box[0] (must be a 'true', 'false', or a valid color string)";
                    throw new HalconException(hv_Exception);
                }
                //Calculate box extents
                hv_String_COPY_INP_TMP = (" " + hv_String_COPY_INP_TMP) + " ";
                hv_Width = new HTuple();
                for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
                    )) - 1); hv_Index = (int)hv_Index + 1)
                {
                    HOperatorSet.GetStringExtents(hv_WindowHandle, hv_String_COPY_INP_TMP.TupleSelect(
                        hv_Index), out hv_Ascent, out hv_Descent, out hv_W, out hv_H);
                    hv_Width = hv_Width.TupleConcat(hv_W);
                }
                hv_FrameHeight = hv_MaxHeight * (new HTuple(hv_String_COPY_INP_TMP.TupleLength()
                    ));
                hv_FrameWidth = (((new HTuple(0)).TupleConcat(hv_Width))).TupleMax();
                hv_R2 = hv_R1 + hv_FrameHeight;
                hv_C2 = hv_C1 + hv_FrameWidth;
                //Display rectangles
                HOperatorSet.GetDraw(hv_WindowHandle, out hv_DrawMode);
                HOperatorSet.SetDraw(hv_WindowHandle, "fill");
                //Set shadow color
                HOperatorSet.SetColor(hv_WindowHandle, hv_ShadowColor);
                if ((int)(hv_UseShadow) != 0)
                {
                    HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1 + 1, hv_C1 + 1, hv_R2 + 1, hv_C2 + 1);
                }
                //Set box color
                HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect(0));
                HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1, hv_C1, hv_R2, hv_C2);
                HOperatorSet.SetDraw(hv_WindowHandle, hv_DrawMode);
            }
            //Write text.
            for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
                )) - 1); hv_Index = (int)hv_Index + 1)
            {
                hv_CurrentColor = hv_Color_COPY_INP_TMP.TupleSelect(hv_Index % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength()
                    )));
                if ((int)((new HTuple(hv_CurrentColor.TupleNotEqual(""))).TupleAnd(new HTuple(hv_CurrentColor.TupleNotEqual(
                    "auto")))) != 0)
                {
                    HOperatorSet.SetColor(hv_WindowHandle, hv_CurrentColor);
                }
                else
                {
                    HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue);
                }
                hv_Row_COPY_INP_TMP = hv_R1 + (hv_MaxHeight * hv_Index);
                HOperatorSet.SetTposition(hv_WindowHandle, hv_Row_COPY_INP_TMP, hv_C1);
                HOperatorSet.WriteString(hv_WindowHandle, hv_String_COPY_INP_TMP.TupleSelect(
                    hv_Index));
            }
            //Reset changed window settings
            HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue);
            HOperatorSet.SetPart(hv_WindowHandle, hv_Row1Part, hv_Column1Part, hv_Row2Part,
                hv_Column2Part);

            return;
        }
        public Form1()
        {
            InitializeComponent();
            HOperatorSet.OpenWindow(0, 0, pictureBox1.Width, pictureBox1.Height, pictureBox1.Handle, "", "", out window);
            HDevWindowStack.Push(window);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            HOperatorSet.ReadImage(out HObject image, "F://halcon//halconStudy//相机标定//1.png");
            HOperatorSet.GetImageSize(image, out HTuple width, out HTuple heigth);
            HOperatorSet.SetPart(window, 0, 0, heigth - 1, width - 1);
            HOperatorSet.DispObj(image, window);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            HOperatorSet.ReadImage(out HObject image, "F://halcon//halconStudy//相机标定//2.png");
            HOperatorSet.GetImageSize(image, out HTuple width, out HTuple heigth);
            HOperatorSet.SetPart(window, 0, 0, heigth - 1, width - 1);
            HOperatorSet.DispObj(image, window);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            HOperatorSet.ReadImage(out HObject image, "F://halcon//halconStudy//相机标定//3.png");
            HOperatorSet.GetImageSize(image, out HTuple width, out HTuple heigth);
            HOperatorSet.SetPart(window, 0, 0, heigth - 1, width - 1);
            HOperatorSet.DispObj(image, window);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            HOperatorSet.ReadImage(out HObject image, "F://halcon//halconStudy//相机标定//4.png");
            HOperatorSet.GetImageSize(image, out HTuple width, out HTuple heigth);
            HOperatorSet.SetPart(window, 0, 0, heigth - 1, width - 1);
            HOperatorSet.DispObj(image, window);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            // Local iconic variables 

            HObject ho_Image20230323135129, ho_GrayImage;
            HObject ho_Regions, ho_RegionOpening, ho_ConnectedRegions;
            HObject ho_ObjectSelected = null;

            // Local control variables 

            HTuple hv_WindowHandle = null, hv_Mean = null;
            HTuple hv_Deviation = null, hv_Number = null, hv_Index = null;
            HTuple hv_Area = new HTuple(), hv_Row = new HTuple(), hv_Column = new HTuple();
            HTuple hv_Mean1 = new HTuple(), hv_Deviation1 = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Image20230323135129);
            HOperatorSet.GenEmptyObj(out ho_GrayImage);
            HOperatorSet.GenEmptyObj(out ho_Regions);
            HOperatorSet.GenEmptyObj(out ho_RegionOpening);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_ObjectSelected);
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.CloseWindow(HDevWindowStack.Pop());
            }
            HOperatorSet.SetWindowAttr("background_color", "black");
            HOperatorSet.OpenWindow(0, 0, 512, 512, 0, "", "", out hv_WindowHandle);
            HDevWindowStack.Push(hv_WindowHandle);
            ho_Image20230323135129.Dispose();
            HOperatorSet.ReadImage(out ho_Image20230323135129, "F:/halcon/halconStudy/image/b.png");
            ho_GrayImage.Dispose();
            HOperatorSet.Rgb1ToGray(ho_Image20230323135129, out ho_GrayImage);
            ho_Regions.Dispose();
            HOperatorSet.Threshold(ho_GrayImage, out ho_Regions, 83, 226);
            ho_RegionOpening.Dispose();
            HOperatorSet.OpeningRectangle1(ho_Regions, out ho_RegionOpening, 10, 10);
            ho_ConnectedRegions.Dispose();
            HOperatorSet.Connection(ho_RegionOpening, out ho_ConnectedRegions);
            HOperatorSet.Intensity(ho_ConnectedRegions, ho_GrayImage, out hv_Mean, out hv_Deviation);

            HOperatorSet.CountObj(ho_ConnectedRegions, out hv_Number);
            HTuple end_val10 = hv_Number;
            HTuple step_val10 = 1;
            for (hv_Index = 1; hv_Index.Continue(end_val10, step_val10); hv_Index = hv_Index.TupleAdd(step_val10))
            {
                ho_ObjectSelected.Dispose();
                HOperatorSet.SelectObj(ho_ConnectedRegions, out ho_ObjectSelected, hv_Index);
                HOperatorSet.AreaCenter(ho_ObjectSelected, out hv_Area, out hv_Row, out hv_Column);
                HOperatorSet.Intensity(ho_ObjectSelected, ho_GrayImage, out hv_Mean1, out hv_Deviation1);
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.DispObj(ho_Image20230323135129, HDevWindowStack.GetActive()
                        );
                }
                if ((int)((new HTuple(hv_Mean1.TupleGreater(217 - 3))).TupleAnd(new HTuple(hv_Mean1.TupleLess(
                    217 + 3)))) != 0)
                {
                    disp_message(hv_WindowHandle, "黄色", "Image20230323135129", hv_Row,
                        hv_Column, "black", "true");
                }
                if ((int)((new HTuple(hv_Mean1.TupleGreater(124 - 1))).TupleAnd(new HTuple(hv_Mean1.TupleLess(
                    124 + 1)))) != 0)
                {
                    disp_message(hv_WindowHandle, "绿色", "Image20230323135129", hv_Row,
                        hv_Column, "black", "true");
                }
                if ((int)((new HTuple(hv_Mean1.TupleGreater(91 - 3))).TupleAnd(new HTuple(hv_Mean1.TupleLess(
                    91 + 3)))) != 0)
                {
                    disp_message(hv_WindowHandle, "红色", "Image20230323135129", hv_Row,
                        hv_Column, "black", "true");
                }
                if ((int)((new HTuple(hv_Mean1.TupleGreater(122 - 1))).TupleAnd(new HTuple(hv_Mean1.TupleLess(
                    122 + 1)))) != 0)
                {
                    disp_message(hv_WindowHandle, "蓝色", "Image20230323135129", hv_Row,
                        hv_Column, "black", "true");
                }
            }
        }
    }
}

如果使用之前导出文件中定义的展示方法,存在显示问题(不在控件中显示)
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值