分享一个自己写的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;