C# 实现图片上下页切换,绘制ROI区域

结果展示:

部分主要代码展示(代码比较重复,懒得整理)

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

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

        // 图片上一张
        private void button3_Click(object sender, EventArgs e)
        {
            i--;

            if (i < 0)
            {
                i = path.Length - 1;

            }
            hWindowControl1.HalconWindow.ClearWindow();
            ho_Image.ReadImage(path[i]);
            int width, height;
            ho_Image.GetImageSize(out width, out height);
            double ratioWidth = (1.0) * width / hWindowControl1.Width;
            double ratioHeight = (1.0) * height / 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;

            }
            else
            {
                row1 = 0;
                column1 = -(1.0) * ((hWindowControl1.Width * ratioHeight) - width) / 2;
                row2 = row1 + hWindowControl1.Height * ratioHeight;
                column2 = column1 + hWindowControl1.Width * ratioHeight;
            }
            HOperatorSet.SetPart(hWindowControl1.HalconWindow, row1, column1, row2, column2);
            hWindowControl1.HalconWindow.DispObj(ho_Image);
        }

        // 加载图片
        private void button1_Click(object sender, EventArgs e)
        {
            hWindowControl1.HalconWindow.ClearWindow();
            ho_Image.ReadImage(@"D:/桌面文件夹/img2/1.jpg");
            int width, height;
            ho_Image.GetImageSize(out width, out height);
            double ratioWidth = (1.0) * width / hWindowControl1.Width;
            double ratioHeight = (1.0) * height / 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;

            }
            else
            {
                row1 = 0;
                column1 = -(1.0) * ((hWindowControl1.Width * ratioHeight) - width) / 2;
                row2 = row1 + hWindowControl1.Height * ratioHeight;
                column2 = column1 + hWindowControl1.Width * ratioHeight;
            }
            HOperatorSet.SetPart(hWindowControl1.HalconWindow, row1, column1, row2, column2);
            hWindowControl1.HalconWindow.DispObj(ho_Image);
        }



        // 获取某个文件夹下的路径
        string[] path = Directory.GetFiles(@"D:/桌面文件夹/img2");
        int i = 0;


        HImage ho_Image = new HImage();

        // 图片下一张
        private void button2_Click(object sender, EventArgs e)
        {
            i++;
            if (i > path.Length - 1)
            {
                i = 0;
            }
            hWindowControl1.HalconWindow.ClearWindow();
            
            ho_Image.ReadImage(path[i]);
            int width, height;
            ho_Image.GetImageSize(out width, out height);
            double ratioWidth = (1.0) * width / hWindowControl1.Width;
            double ratioHeight = (1.0) * height / 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;

            }
            else
            {
                row1 = 0;
                column1 = -(1.0) * ((hWindowControl1.Width * ratioHeight) - width) / 2;
                row2 = row1 + hWindowControl1.Height * ratioHeight;
                column2 = column1 + hWindowControl1.Width * ratioHeight;
            }
            HOperatorSet.SetPart(hWindowControl1.HalconWindow, row1, column1, row2, column2);
            hWindowControl1.HalconWindow.DispObj(ho_Image);

        }

        // 绘制ROI区域
        private void button4_Click(object sender, EventArgs e)
        {
            hWindowControl1.Focus(); //光标
            hWindowControl1.HalconWindow.DrawCircle(out double row, out double column, out double radius);
            HRegion region = new HRegion();
            
            region.GenCircle(row, column, radius);
            hWindowControl1.HalconWindow.SetColor("green");
            region.DispRegion(hWindowControl1.HalconWindow);

        }



        // 保存图片
        private void button5_Click(object sender, EventArgs e)
        {
            ho_Image.WriteImage("bmp", 0, @"D:/桌面文件夹/img2/1.bmp");
        }
    }



}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

挽风起苍岚

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

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

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

打赏作者

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

抵扣说明:

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

余额充值