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

2024-06-01 16:44

本文主要是介绍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】

这篇关于C#使用GDI对一个矩形进行任意角度旋转的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1021553

相关文章

C#连接SQL server数据库命令的基本步骤

《C#连接SQLserver数据库命令的基本步骤》文章讲解了连接SQLServer数据库的步骤,包括引入命名空间、构建连接字符串、使用SqlConnection和SqlCommand执行SQL操作,... 目录建议配合使用:如何下载和安装SQL server数据库-CSDN博客1. 引入必要的命名空间2.

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

SpringBoot结合Docker进行容器化处理指南

《SpringBoot结合Docker进行容器化处理指南》在当今快速发展的软件工程领域,SpringBoot和Docker已经成为现代Java开发者的必备工具,本文将深入讲解如何将一个SpringBo... 目录前言一、为什么选择 Spring Bootjavascript + docker1. 快速部署与

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

linux解压缩 xxx.jar文件进行内部操作过程

《linux解压缩xxx.jar文件进行内部操作过程》:本文主要介绍linux解压缩xxx.jar文件进行内部操作,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、解压文件二、压缩文件总结一、解压文件1、把 xxx.jar 文件放在服务器上,并进入当前目录#

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件