当前位置: 首页 > news >正文

C#使用GDI对一个矩形进行任意角度旋转

C#对一个矩形进行旋转GDI绘图,可以指定任意角度进行旋转

我们可以认为一张图片Image,本质就是一个矩形Rectangle,旋转矩形也就是旋转图片

在画图密封类 System.Drawing.Graphics中,

矩形旋转的两个关键方法

//设置旋转的中心点

public void TranslateTransform(float dx, float dy);

//旋转指定的角度【单位°】:旋转角度 从 X+ 到 Y+之间的旋转角度认为是正数

public void RotateTransform(float angle);

矩形旋转WindowsForms应用程序

新建窗体应用程序RotatedRectangleDemo,将默认的Form1重命名为FormRotatedRectangle

窗体FormRotatedRectangle设计如图:

窗体设计FormRotatedRectangle设计器代码如下:

文件FormRotatedRectangle.Designer.cs


namespace RotatedRectangleDemo
{partial class FormRotatedRectangle{/// <summary>/// 必需的设计器变量。/// </summary>private System.ComponentModel.IContainer components = null;/// <summary>/// 清理所有正在使用的资源。/// </summary>/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows 窗体设计器生成的代码/// <summary>/// 设计器支持所需的方法 - 不要修改/// 使用代码编辑器修改此方法的内容。/// </summary>private void InitializeComponent(){this.btnRotate = new System.Windows.Forms.Button();this.label1 = new System.Windows.Forms.Label();this.numRotate = new System.Windows.Forms.NumericUpDown();((System.ComponentModel.ISupportInitialize)(this.numRotate)).BeginInit();this.SuspendLayout();// // btnRotate// this.btnRotate.Font = new System.Drawing.Font("宋体", 13F);this.btnRotate.Location = new System.Drawing.Point(271, 7);this.btnRotate.Name = "btnRotate";this.btnRotate.Size = new System.Drawing.Size(75, 33);this.btnRotate.TabIndex = 0;this.btnRotate.Text = "旋转";this.btnRotate.UseVisualStyleBackColor = true;this.btnRotate.Click += new System.EventHandler(this.btnRotate_Click);// // label1// this.label1.AutoSize = true;this.label1.Location = new System.Drawing.Point(25, 20);this.label1.Name = "label1";this.label1.Size = new System.Drawing.Size(119, 12);this.label1.TabIndex = 1;this.label1.Text = "旋转角度(0°~360°)";// // numRotate// this.numRotate.DecimalPlaces = 2;this.numRotate.Location = new System.Drawing.Point(150, 16);this.numRotate.Maximum = new decimal(new int[] {360,0,0,0});this.numRotate.Name = "numRotate";this.numRotate.Size = new System.Drawing.Size(97, 21);this.numRotate.TabIndex = 2;this.numRotate.Value = new decimal(new int[] {30,0,0,0});// // FormRotatedRectangle// this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.ClientSize = new System.Drawing.Size(945, 508);this.Controls.Add(this.numRotate);this.Controls.Add(this.label1);this.Controls.Add(this.btnRotate);this.Name = "FormRotatedRectangle";this.Text = "GDI旋转示例:矩形旋转任意角度";((System.ComponentModel.ISupportInitialize)(this.numRotate)).EndInit();this.ResumeLayout(false);this.PerformLayout();}#endregionprivate System.Windows.Forms.Button btnRotate;private System.Windows.Forms.Label label1;private System.Windows.Forms.NumericUpDown numRotate;}
}

旋转矩形示例代码如下:

文件FormRotatedRectangle.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace RotatedRectangleDemo
{public partial class FormRotatedRectangle : Form{public FormRotatedRectangle(){InitializeComponent();/* 定义了一个名为FormRotatedRectangle的窗体类,* 它覆盖(override)了OnPaint方法来绘制旋转的矩形。* 在OnPaint方法中,我们首先设置矩形的位置和大小,然后保存图形状态,* 接着设置旋转变换的中心点,通过RotateTransform方法设置旋转角度,并绘制旋转后的矩形,最后恢复图形状态。* 程序运行时,会显示一个旋转了30度的矩形。*/}/// <summary>/// 窗体重绘事件/// </summary>/// <param name="e"></param>protected override void OnPaint(PaintEventArgs e){base.OnPaint(e);Graphics g = e.Graphics;// 设置矩形的位置和大小Rectangle rect = new Rectangle(100, 100, 200, 100);RotateRectangle(g, rect, (float)numRotate.Value);}/// <summary>/// 将初始矩形旋转指定的角度数/// </summary>/// <param name="g">窗体画图对象</param>/// <param name="rect">初始矩形对象(x,y,width,height)</param>/// <param name="rotateAngle">旋转角度,单位°</param>private void RotateRectangle(Graphics g, Rectangle rect, float rotateAngle) {// 保存原始状态GraphicsState state = g.Save();// 设置旋转的中心点为矩形的中心g.TranslateTransform(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);// 旋转矩形:旋转角度 从 X+ 到 Y+之间的旋转角度认为是正数g.RotateTransform(rotateAngle);// 恢复变换的中心点g.TranslateTransform(-(rect.X + rect.Width / 2), -(rect.Y + rect.Height / 2));// 绘制矩形g.DrawRectangle(Pens.Black, rect);// 恢复图形状态g.Restore(state);}private void btnRotate_Click(object sender, EventArgs e){this.Invalidate();//引起触发重绘OnPaint事件}}
}

程序运行如图:

旋转45°【π/4】

旋转90°【π/2】

相关文章:

  • exe语言编程:深入探索与挑战未知
  • 香橙派OrangePI AiPro测评 【运行qt,编解码,xfreeRDP】
  • 49、Floyd求最短路
  • 4K高刷显示器 - 蚂蚁电竞ANT27VU
  • Swift 并发
  • 机器学习模型以及优缺点——logistic
  • java基础-chapter18(网络编程)
  • TreeMap和TreeSet的排序机制
  • 第十四章 创建Web客户端 - XML 命名空间的 SOAP 向导选项
  • 【第2章】SpringBoot实战篇之接口参数校验和全局异常处理
  • linux上VirtualBox使用
  • 原码一位乘法(计算机组成原理)
  • “华为杯”第十三届中国研究生 数学建模竞赛-D题:军事行动避空侦察的时机和路径选择(续)(附MATLAB代码实现)
  • macbook配置前端环境:深度解析与实战指南
  • Arrays(操作数组工具类)、Lambda表达式
  • (十五)java多线程之并发集合ArrayBlockingQueue
  • FastReport在线报表设计器工作原理
  • github从入门到放弃(1)
  • JavaScript 事件——“事件类型”中“HTML5事件”的注意要点
  • Node.js 新计划:使用 V8 snapshot 将启动速度提升 8 倍
  • PAT A1050
  • Python进阶细节
  • Webpack 4x 之路 ( 四 )
  • 从零到一:用Phaser.js写意地开发小游戏(Chapter 3 - 加载游戏资源)
  • 基于游标的分页接口实现
  • 如何在 Tornado 中实现 Middleware
  • 网络应用优化——时延与带宽
  • 吴恩达Deep Learning课程练习题参考答案——R语言版
  • 小而合理的前端理论:rscss和rsjs
  • ​Linux·i2c驱动架构​
  • ​ssh-keyscan命令--Linux命令应用大词典729个命令解读
  • ‌前端列表展示1000条大量数据时,后端通常需要进行一定的处理。‌
  • #{} 和 ${}区别
  • #if #elif #endif
  • $forceUpdate()函数
  • (1)STL算法之遍历容器
  • (NSDate) 时间 (time )比较
  • (回溯) LeetCode 40. 组合总和II
  • (三)模仿学习-Action数据的模仿
  • (数位dp) 算法竞赛入门到进阶 书本题集
  • (一)springboot2.7.6集成activit5.23.0之集成引擎
  • (原创) cocos2dx使用Curl连接网络(客户端)
  • ***原理与防范
  • .bat批处理(七):PC端从手机内复制文件到本地
  • .NET 2.0中新增的一些TryGet,TryParse等方法
  • .NET delegate 委托 、 Event 事件,接口回调
  • .net FrameWork简介,数组,枚举
  • .NET 常见的偏门问题
  • .net 怎么循环得到数组里的值_关于js数组
  • .NET分布式缓存Memcached从入门到实战
  • [.net 面向对象程序设计进阶] (19) 异步(Asynchronous) 使用异步创建快速响应和可伸缩性的应用程序...
  • [2013AAA]On a fractional nonlinear hyperbolic equation arising from relative theory
  • [AI StoryDiffusion] 创造神奇故事,AI漫画大乱斗!
  • [BZOJ4010]菜肴制作
  • [C#] 我的log4net使用手册