c#截取旋转矩形区域内的图像
先来看看最终效果。
因为实际项目中要用到opencvsharp,所以这里面的旋转矩形就直接用opencvsharp里面封装好了的RotatedRect。
private Size2f cropSize = new Size2f(400, 300);
private RotatedRect cropRect = new RotatedRect();
private PictureBox cropRectBox;
private SolidBrush brush;
public Form1()
{
InitializeComponent();
brush = new SolidBrush(Color.FromArgb(50, 255, 0, 0));
cropRectBox = new PictureBox()
{
Visible = false,
BackColor = Color.Transparent,
SizeMode = PictureBoxSizeMode.Normal,
BorderStyle = BorderStyle.None,
Parent = rawImgPb
};
cropRectBox.MouseDown += CropRectBox_MouseDown;
rawImgPb.Image = Image.FromFile("C:\\Users\\Administrator\\Desktop\\测试\\1.jpg");
}
private void CropRectBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right)
return;
cropRect.Angle += 10;
ShowCropRectBox(cropRect);
}
private void rawImgPb_MouseDown(object sender, MouseEventArgs e)
{
cropRect.Center = new Point2f(e.X + cropSize.Width / 2, e.Y + cropSize.Height / 2);
cropRect.Size = cropSize;
cropRect.Angle = 0;
ShowCropRectBox(cropRect)