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");
        }
    }



}

猜你喜欢

转载自blog.csdn.net/qq_62238325/article/details/133915123