C# 编写ROI截图工具

分享一个自己写的ROI截图工具,可以实现图片截取部位ROI绘制,点击截图按钮可以自动保存,代码如下:

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

namespace Tools
{
    public partial class RectROI : Form
    {
        private Bitmap originalImage;
        private float currentScale = 1.0f;
        private RectangleF roi = RectangleF.Empty;
        private bool isDragging = false;
        private bool isResizing = false;
        private bool isDrawing = false;
        private PointF dragStartPosition;
        private RectangleF dragStartRoi;
        private ResizeHandle resizeHandle = ResizeHandle.None;
        private StatusStrip statusStrip1;
        private ToolStripStatusLabel toolStripStatusLabel1;
        private Button button1;
        private Button button2;
        private RadioButton radioButton1;
        private RadioButton radioButton2;
        private RadioButton radioButton3;
        private CheckBox checkBox1;
        private RadioButton radioButton4;
        private PointF drawStartPoint;

        public RectROI()
        {
            InitializeComponent();
            pictureBox.MouseWheel += PictureBox_MouseWheel;
            pictureBox.MouseDown += PictureBox_MouseDown;
            pictureBox.MouseMove += PictureBox_MouseMove;
            pictureBox.MouseUp += PictureBox_MouseUp;
            pictureBox.Paint += PictureBox_Paint;
            pictureBox.SizeMode = PictureBoxSizeMode.Normal;
            panel.AutoScroll = true;
            this.DoubleBuffered = true;
        }

        private void LoadImageButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                originalImage = new Bitmap(openFile.FileName);
                currentScale = 1.0f;
                pictureBox.Size = originalImage.Size;
                roi = RectangleF.Empty;
                pictureBox.Invalidate();
            }
        }

        private void PictureBox_MouseWheel(object sender, MouseEventArgs e)
        {
            if (originalImage == null) return;

            float scaleFactor = e.Delta > 0 ? 1.1f : 0.9f;
            currentScale *= scaleFactor;
            currentScale = Math.Max(0.1f, Math.Min(currentScale, 10f));

            int newWidth = (int)(originalImage.Width * currentScale);
            int newHeight = (int)(originalImage.Height * currentScale);
            pictureBox.Size = new Size(newWidth, newHeight);
            pictureBox.Invalidate();
        }

        private void PictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (originalImage == null) return;

            float originalX = e.X / currentScale;
            float originalY = e.Y / currentScale;
            PointF mousePos = new PointF(originalX, originalY);

            if (roi.IsEmpty)
            {
                isDrawing = true;
                drawStartPoint = mousePos;
                roi = new RectangleF(drawStartPoint.X, drawStartPoint.Y, 0, 0);
            }
            else
            {
                resizeHandle = GetResizeHandle(mousePos);
                if (resizeHandle != ResizeHandle.None)
                {
                    isResizing = true;
                    dragStartPosition = mousePos;
                    dragStartRoi = roi;
                }
                else if (roi.Contains(mousePos))
                {
                    isDragging = true;
                    dragStartPosition = mousePos;
                    dragStartRoi = roi;
                }
            }

            if (isResizing || isDragging || isDrawing)
                pictureBox.Capture = true;
        }

        private ResizeHandle GetResizeHandle(PointF mousePos)
        {
            float handleSize = 8 / currentScale;
            RectangleF[] handles = new RectangleF[8];
            // 四个角控制点
            handles[0] = new RectangleF(roi.Left - handleSize / 2, roi.Top - handleSize / 2, handleSize, handleSize);
            handles[1] = new RectangleF(roi.Right - handleSize / 2, roi.Top - handleSize / 2, handleSize, handleSize);
            handles[2] = new RectangleF(roi.Left - handleSize / 2, roi.Bottom - handleSize / 2, handleSize, handleSize);
            handles[3] = new RectangleF(roi.Right - handleSize / 2, roi.Bottom - handleSize / 2, handleSize, handleSize);

            // 四个边中点控制点
            handles[4] = new RectangleF(roi.Left + roi.Width / 2 - handleSize / 2, roi.Top- handleSize / 2, handleSize, handleSize);//TOP
            handles[5] = new RectangleF(roi.Left + roi.Width / 2 - handleSize / 2, roi.Bottom - handleSize / 2, handleSize, handleSize);//BOTTOM
            handles[6] = new RectangleF(roi.Left - handleSize / 2, roi.Top+roi.Height/2- handleSize / 2, handleSize, handleSize);//LEFT
            handles[7] = new RectangleF(roi.Right - handleSize / 2, roi.Top + roi.Height / 2 - handleSize / 2, handleSize, handleSize);//RIGHT

            for (int i = 0; i < handles.Length; i++)
                if (handles[i].Contains(mousePos))
                    return (ResizeHandle)(i + 1);

            return ResizeHandle.None;
     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值