C#利用GDI+绘制旋转文字等效果
C#中利用GDI+绘制旋转文本的文字,网上有很多资料,基本都使用矩阵旋转的方式实现。但基本都只提及按点旋转,若要实现在矩形范围内旋转文本,资料较少。经过琢磨,可以将矩形内旋转转化为按点旋转,不过需要经过不少的计算过程。利用下面的类可以实现该功能。
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- namespace RotateText
- {
- public class GraphicsText
- {
- private Graphics _graphics;
- public GraphicsText()
- {
- }
- public Graphics Graphics
- {
- get { return _graphics; }
- set { _graphics = value; }
- }
- /// <summary>
- /// 绘制根据矩形旋转文本
- /// </summary>
- /// <param name="s">文本</param>
- /// <param name="font">字体</param>
- /// <param name="brush">填充</param>
- /// <param name="layoutRectangle">局部矩形</param>
- /// <param name="format">布局方式</param>
- /// <param name="angle">角度</param>
- public void DrawString(string s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format, float angle)
- {
- // 求取字符串大小
- SizeF size = _graphics.MeasureString(s, font);
- // 根据旋转角度,求取旋转后字符串大小
- SizeF sizeRotate = ConvertSize(size, angle);
- // 根据旋转后尺寸、布局矩形、布局方式计算文本旋转点
- PointF rotatePt = GetRotatePoint(sizeRotate, layoutRectangle, format);
- // 重设布局方式都为Center
- StringFormat newFormat = new StringFormat(format);
- newFormat.Alignment = StringAlignment.Center;
- newFormat.LineAlignment = StringAlignment.Center;
- // 绘制旋转后文本
- DrawString(s, font, brush, rotatePt, newFormat, angle);
- }
- /// <summary>
- /// 绘制根据点旋转文本,一般旋转点给定位文本包围盒中心点
- /// </summary>
- /// <param name="s">文本</param>
- /// <param name="font">字体</param>
- /// <param name="brush">填充</param>
- /// <param name="point">旋转点</param>
- /// <param name="format">布局方式</param>
- /// <param name="angle">角度</param>
- public void DrawString(string s, Font font, Brush brush, PointF point, StringFormat format, float angle)
- {
- // Save the matrix
- Matrix mtxSave = _graphics.Transform;
- Matrix mtxRotate = _graphics.Transform;
- mtxRotate.RotateAt(angle, point);
- _graphics.Transform = mtxRotate;
- _graphics.DrawString(s, font, brush, point, format);
- // Reset the matrix
- _graphics.Transform = mtxSave;
- }
- private SizeF ConvertSize(SizeF size, float angle)
- {
- Matrix matrix = new Matrix();
- matrix.Rotate(angle);
- // 旋转矩形四个顶点
- PointF[] pts = new PointF[4];
- pts[0].X = -size.Width / 2f;
- pts[0].Y = -size.Height / 2f;
- pts[1].X = -size.Width / 2f;
- pts[1].Y = size.Height / 2f;
- pts[2].X = size.Width / 2f;
- pts[2].Y = size.Height / 2f;
- pts[3].X = size.Width / 2f;
- pts[3].Y = -size.Height / 2f;
- matrix.TransformPoints(pts);
- // 求取四个顶点的包围盒
- float left = float.MaxValue;
- float right = float.MinValue;
- float top = float.MaxValue;
- float bottom = float.MinValue;
- foreach(PointF pt in pts)
- {
- // 求取并集
- if(pt.X < left)
- left = pt.X;
- if(pt.X > right)
- right = pt.X;
- if(pt.Y < top)
- top = pt.Y;
- if(pt.Y > bottom)
- bottom = pt.Y;
- }
- SizeF result = new SizeF(right - left, bottom - top);
- return result;
- }
- private PointF GetRotatePoint(SizeF size, RectangleF layoutRectangle, StringFormat format)
- {
- PointF pt = new PointF();
- switch (format.Alignment)
- {
- case StringAlignment.Near:
- pt.X = layoutRectangle.Left + size.Width / 2f;
- break;
- case StringAlignment.Center:
- pt.X = (layoutRectangle.Left + layoutRectangle.Right) / 2f;
- break;
- case StringAlignment.Far:
- pt.X = layoutRectangle.Right - size.Width / 2f;
- break;
- default:
- break;
- }
- switch (format.LineAlignment)
- {
- case StringAlignment.Near:
- pt.Y = layoutRectangle.Top + size.Height / 2f;
- break;
- case StringAlignment.Center:
- pt.Y = (layoutRectangle.Top + layoutRectangle.Bottom) / 2f;
- break;
- case StringAlignment.Far:
- pt.Y = layoutRectangle.Bottom - size.Height / 2f;
- break;
- default:
- break;
- }
- return pt;
- }
- }
- }
测试代码如下:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Windows.Forms;
- namespace RotateText
- {
- public partial class FormMain : Form
- {
- private Font _font = new Font("Arial", 12);
- private Brush _brush = new SolidBrush(Color.Black);
- private Pen _pen = new Pen(Color.Black, 1f);
- private string _text = "Crow Soft";
- public FormMain()
- {
- InitializeComponent();
- }
- protected override void OnPaint(PaintEventArgs e)
- {
- base.OnPaint(e);
- GraphicsText graphicsText = new GraphicsText();
- graphicsText.Graphics = e.Graphics;
- // 绘制围绕点旋转的文本
- StringFormat format = new StringFormat();
- format.Alignment = StringAlignment.Center;
- format.LineAlignment = StringAlignment.Center;
- graphicsText.DrawString(_text, _font, _brush, new PointF(100, 80), format, 45f);
- graphicsText.DrawString(_text, _font, _brush, new PointF(200, 80), format, -45f);
- graphicsText.DrawString(_text, _font, _brush, new PointF(300, 80), format, 90f);
- graphicsText.DrawString(_text, _font, _brush, new PointF(400, 80), format, -60f);
- // 绘制矩形内旋转的文本
- // First line
- RectangleF rc = RectangleF.FromLTRB(50, 150, 200, 230);
- RectangleF rect = rc;
- format.Alignment = StringAlignment.Near;
- e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
- graphicsText.DrawString(_text, _font, _brush, rect, format, 30);
- rect.Location += new SizeF(180, 0);
- format.LineAlignment = StringAlignment.Near;
- e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
- graphicsText.DrawString(_text, _font, _brush, rect, format, -30);
- rect.Location += new SizeF(180, 0);
- format.LineAlignment = StringAlignment.Center;
- e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
- graphicsText.DrawString(_text, _font, _brush, rect, format, -90);
- rect.Location += new SizeF(180, 0);
- format.LineAlignment = StringAlignment.Far;
- e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
- graphicsText.DrawString(_text, _font, _brush, rect, format, 70);
- // Second line
- rect = rc;
- rect.Location += new SizeF(0, 100);
- format.Alignment = StringAlignment.Center;
- e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
- graphicsText.DrawString(_text, _font, _brush, rect, format, 40);
- rect.Location += new SizeF(180, 0);
- format.LineAlignment = StringAlignment.Near;
- e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
- graphicsText.DrawString(_text, _font, _brush, rect, format, 30);
- rect.Location += new SizeF(180, 0);
- format.LineAlignment = StringAlignment.Center;
- e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
- graphicsText.DrawString(_text, _font, _brush, rect, format, -70);
- rect.Location += new SizeF(180, 0);
- format.LineAlignment = StringAlignment.Far;
- e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
- graphicsText.DrawString(_text, _font, _brush, rect, format, 60);
- // Third line
- rect = rc;
- rect.Location += new SizeF(0, 200);
- format.Alignment = StringAlignment.Far;
- e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
- graphicsText.DrawString(_text, _font, _brush, rect, format, -30);
- rect.Location += new SizeF(180, 0);
- format.LineAlignment = StringAlignment.Near;
- e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
- graphicsText.DrawString(_text, _font, _brush, rect, format, -30);
- rect.Location += new SizeF(180, 0);
- format.LineAlignment = StringAlignment.Center;
- e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
- graphicsText.DrawString(_text, _font, _brush, rect, format, 90);
- rect.Location += new SizeF(180, 0);
- format.LineAlignment = StringAlignment.Far;
- e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
- graphicsText.DrawString(_text, _font, _brush, rect, format, 45);
- }
- }
- }
效果如下图:
资源地址:http://download.csdn.net/detail/alicehyxx/6626473
C#利用GDI+绘制旋转文字等效果的更多相关文章
- 测试canvas绘制旋转文字的性能
canvas 绘制各种动画效果时,我们经常会使用画布旋转,使绘制上去的元素有旋转的效果. 最近在项目中碰到了很严重的性能问题,经常排查发现是因为绘制批量文字时使用了画布旋转,且每行文字的旋转角度是不一 ...
- CSS环绕球体的旋转文字-3D效果
代码地址如下:http://www.demodashi.com/demo/12482.html 项目文件结构截图 只需要一个html文件既可: 项目截图: 代码实现原理: 该示例的实现过程很简单,主要 ...
- 自定义控件使用GDI+绘制旋转Label文字
http://www.cnblogs.com/CUIT-DX037/ 1.添加用户控件: 2.添加代码: public partial class UcLabel : UserControl { pu ...
- GDI+简单现实文字旋转
原文 http://www.cnblogs.com/kaixiangbb/p/3301272.html 题记 入职新公司已快有两月了,试用期已快结束,项目却迟迟还未正式启动.安排给我的多是些琐事,一直 ...
- GDI绘制时钟效果,与系统时间保持同步,基于Winform
2018年工作之余,想起来捡起GDI方面的技术,特意在RichCodeBox项目中做了两个示例程序,其中一个就是时钟效果,纯C#开发.这个CSharpQuartz是今天上午抽出一些时间,编写的,算是偷 ...
- Winform GDI+绘图二:绘制旋转太极图
大家好,今天有时间给大家带来Winform自绘控件的第二部分,也是比较有意思的一个控件:旋转太极图. 大家可以停下思考一下,如果让你来绘制旋转的太极图,大家有什么样的思路呢?我今天跟大家展示一下,我平 ...
- 学习笔记:利用GDI+生成简单的验证码图片
学习笔记:利用GDI+生成简单的验证码图片 /// <summary> /// 单击图片时切换图片 /// </summary> /// <param name=&quo ...
- CSS3绘制旋转的太极图案(一)
实现步骤: 基础HTML: <div class="box-taiji"> <div class="circle-01">< ...
- 利用QPainter绘制各种图形(Shape, Pen 宽带,颜色,风格,Cap,Join,刷子)
利用QPainter绘制各种图形 Qt的二维图形引擎是基于QPainter类的.QPainter既可以绘制几何形状(点.线.矩形.椭圆.弧形.弦形.饼状图.多边形和贝塞尔曲线),也可以绘制像素映射.图 ...
随机推荐
- Delphi 提示在Delphi的IDE中,按Ctrl+Shift+G键可以为一个接口生成一个新的GUID。
对于Object Pascal语言来说,最近一段时间最有意义的改进就是从Delphi3开始支持接口(interface),接口定义了能够与一个对象进行交互操作的一组过程和函数.对一个接口进行定义包含两 ...
- javascript防止SQL注入
<SCRIPT language="javascript">function Check(theform){ if (theform.UserName.value== ...
- 序列for循环语句
序列for循环语句 序列for循环语句允许重复遍历一组序列,而这组序列可以是任何可以重复遍历的序列,如由begin()和end()函数定义的STL序列.所有的标准容器都可用作这种序列,同时它也同样可以 ...
- 图解WPF程序打包全过程
首先打开已经完成的工程,如图: 下面开始制作安装程序包. 第一步:[文件]——[新建]——[项目]——安装项目. 名称——可以自己根据要求修改. 位置——是指你要制作的安装文件存放在什么目录内,可以根 ...
- Debian openvpn 配置
1.安装openvpn 和 iptables -- Debain 可以使用命令行`apt-get install openvpn iptables` 2.配置服务器 -- ```shell cp -R ...
- 解决SQL Server Always 日志增大的问题-摘自网络
配置了Alwayson之后,因为没有只能使用完全恢复模式,不能使用简单或大容量日志模式,所以日志不断增长,不能使用改变恢复模式的方式清空日志 手动操作收缩或截断日志也无效 读了一些文章后发现,有人使用 ...
- 纯JS Web在线可拖拽的流程设计器
F2工作流引擎之-纯JS Web在线可拖拽的流程设计器 Web纯JS流程设计器无需编程,完全是通过鼠标拖.拉.拽的方式来完成,支持串行.并行.分支.异或分支.M取N路分支.会签.聚合.多重聚合.退回. ...
- NSLog中的%@
[NSLog中的%@] There is one additional substitution token available in Objective-C, %@, used to denote ...
- POJ 3304 Segments (直线和线段相交判断)
Segments Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7739 Accepted: 2316 Descript ...
- Labview中局部变量和全局变量
局部变量的作用域是整个VI,它用于在单个VI中传输数据: 全局变量的作用域是整台计算机,它主要用于多个VI之间共享数据