C# 绘制旋转矩形 以及截取旋转矩形内的图像

本文介绍了如何在C#中绘制旋转矩形,并详细讲解了如何截取旋转矩形内部的图像,包括旋转矩形的实现代码和截图的步骤,展示了具体的效果图。
摘要由CSDN通过智能技术生成

1.旋转矩形

    我参考了这篇 文章 里xuzuning的方法,写一个旋转矩形的方法。

旋转矩形代码:  

private void Rect2Pointfs(Rectangle rect, float angle, out PointF[] lpfs)
        {
            using (var graph = new GraphicsPath())
            {
                Point Center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
                graph.AddRectangle(rect);
                var a = -angle * (Math.PI / 180);
                var n1 = (float)Math.Cos(a);
                var n2 = (float)Math.Sin(a);
                var n3 = -(float)Math.Sin(a);
                var n4 = (float)Math.Cos(a);
                var n5 = (float)(Center.X * (1 - Math.Cos(a)) + Center.Y * Math.Sin(a));
                var n6 = (float)(Center.Y * (1 - Math.Cos(a))
  • 6
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 16
    评论
好的,以下是C# Emgucv绘制旋转矩形的代码: ```csharp using Emgu.CV; using Emgu.CV.Structure; using System.Drawing; // 定义旋转矩形的中心点、宽度、高度和旋转角度 PointF center = new PointF(100, 100); float width = 200; float height = 100; float angle = 45; // 创建一个空白的图像 Image<Bgr, byte> img = new Image<Bgr, byte>(500, 500, new Bgr(255, 255, 255)); // 计算旋转矩形的四个顶点 PointF[] vertices = new PointF[4]; vertices[0] = new PointF(center.X - width / 2, center.Y - height / 2); vertices[1] = new PointF(center.X + width / 2, center.Y - height / 2); vertices[2] = new PointF(center.X + width / 2, center.Y + height / 2); vertices[3] = new PointF(center.X - width / 2, center.Y + height / 2); // 创建旋转矩阵 Matrix<float> rotationMatrix = new Matrix<float>(new float[,] { { (float)Math.Cos(angle * Math.PI / 180), (float)Math.Sin(angle * Math.PI / 180), 0 }, { -(float)Math.Sin(angle * Math.PI / 180), (float)Math.Cos(angle * Math.PI / 180), 0 }, { 0, 0, 1 } }); // 将旋转矩阵应用于顶点 rotationMatrix.TransformPoints(vertices); // 绘制旋转矩形 img.DrawPolyline(vertices, true, new Bgr(0, 0, 255), 2); // 显示图像 CvInvoke.Imshow("Rotated Rectangle", img); CvInvoke.WaitKey(0); ``` 这段代码首先定义了旋转矩形的中心点、宽度、高度和旋转角度,然后创建了一个空白的图像。接着计算旋转矩形的四个顶点,并创建旋转矩阵。最后将旋转矩阵应用于顶点,绘制旋转矩形,并显示图像
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值