VS2022 C# 自定义用户控件(PictureBox)

1. 创建自定义控件(PictureBox)

1.1 新建自定义控件类库

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

1.2 添加pictureBox控件

可自己设置一些属性,比如背景色等等
在这里插入图片描述
添加“halcondonet.dll”
在这里插入图片描述

1.3 写显示图像的程序代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;

namespace FormsControl
{
    public partial class UserControl1: UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }
        #region 全局变量

        HTuple hv_Width, hv_Height;
        HTuple hv_Windowld;
        HObject curImage;
        #endregion

        /// <summary>
        /// 输入图像参数,显示
        /// </summary>
        /// <param name="timage"></param>输入图像
        /// <exception cref="Exception"></exception>
        public void ShowImage(HObject tImage)
        {
            HOperatorSet.OpenWindow(0, 0, pictureBox1.Width, pictureBox1.Height, pictureBox1.Handle, "", "", out  hv_Windowld);
            //获取图像大小
            HOperatorSet.ClearWindow(hv_Windowld);
            HOperatorSet.GetImageSize(tImage, out hv_Width, out hv_Height);
            //设置在窗口中显示图像
            HOperatorSet.SetPart(hv_Windowld, 0, 0, hv_Height, hv_Width);
            HOperatorSet.DispObj(tImage, hv_Windowld);

        }

        public void ShowImage(string imagefile)
        {
            HObject tImage;
            HOperatorSet.ReadImage(out tImage, imagefile);
            HOperatorSet.OpenWindow(0, 0, pictureBox1.Width, pictureBox1.Height, pictureBox1.Handle, "", "", out hv_Windowld);
            //获取图像大小
            HOperatorSet.ClearWindow(hv_Windowld);
            HOperatorSet.GetImageSize(tImage, out hv_Width, out hv_Height);
            //设置在窗口中显示图像
            HOperatorSet.SetPart(hv_Windowld, 0, 0, hv_Height, hv_Width);
            HOperatorSet.DispObj(tImage, hv_Windowld);
        }
    }
}

1.4 生成dll

在这里插入图片描述

2. 调用自定义的控件

2.1 新建窗体应用

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

2.2 添加控件dll

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
添加完成后,拖动控件到Form窗体中,再加两个测试显示功能的按钮
在这里插入图片描述

程序代码

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 TestUserControl
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string FileName = @"E:\Programming\VS2017\C#TestExample\TestUserControl\dollar.bmp";
            userControl11.ShowImage(FileName);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string FileName = @"E:\Programming\VS2017\C#TestExample\TestUserControl\dollar.bmp";
            HOperatorSet.ReadImage(out HObject image,FileName);
            userControl11.ShowImage(image);
        }
    }
}

若出现下图所示的错误
在这里插入图片描述
将目标平台改成 X64
在这里插入图片描述

2.3 完成自定义控件的调用

在这里插入图片描述

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C# 中,可以使用 PictureBox 控件或者自定义控件来显示图片,以下是两种方法的示例: 1. 使用 PictureBox 控件 PictureBox 控件是 Windows 窗体应用程序中用于显示图片的常用控件。你可以通过以下步骤在窗体中添加一个 PictureBox 控件: - 打开 Visual Studio,创建一个新的 Windows 窗体应用程序项目。 - 在“工具箱”中找到 PictureBox 控件,拖动它到窗体上。 - 右键单击 PictureBox 控件,在“属性”窗口中设置其 Image 属性为要显示的图片。 下面是一个简单的示例代码: ```csharp using System; using System.Windows.Forms; namespace PictureBoxExample { public partial class Form1 : Form { public Form1() { InitializeComponent(); // 设置 PictureBox 的 Image 属性为要显示的图片 pictureBox1.Image = Image.FromFile("path/to/image.jpg"); } } } ``` 2. 使用自定义控件 如果你需要更多的图片显示特性,可以考虑使用自定义控件。以下是一个简单的示例代码: ```csharp using System; using System.Drawing; using System.Windows.Forms; namespace CustomControlExample { public class ImageControl : Control { private Image image; public Image Image { get { return image; } set { image = value; Invalidate(); } } protected override void OnPaint(PaintEventArgs e) { // 在控件上绘制图片 if (image != null) { e.Graphics.DrawImage(image, 0, 0); } } } public partial class Form1 : Form { public Form1() { InitializeComponent(); // 创建自定义控件并设置其 Image 属性为要显示的图片 var imageControl = new ImageControl(); imageControl.Image = Image.FromFile("path/to/image.jpg"); // 将自定义控件添加到窗体中 Controls.Add(imageControl); } } } ``` 在上面的代码中,我们创建了一个名为 ImageControl 的自定义控件,并在其中添加了一个 Image 属性来设置要显示的图片。在 OnPaint 方法中,我们通过 Graphics.DrawImage 方法来绘制图片。 在窗体的构造函数中,我们创建了一个 ImageControl 实例并设置其 Image 属性为要显示的图片,然后将该控件添加到窗体中。 注意,自定义控件的绘制逻辑可能比 PictureBox 控件复杂,需要处理一些更多的特性,比如图片的缩放、移动等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值