C#利用GDI+繪制旋轉(zhuǎn)文字等效果實(shí)例
本文實(shí)例講述了C#利用GDI+繪制旋轉(zhuǎn)文字等效果的方法,是非常實(shí)用的技巧。分享給大家供大家參考之用。具體如下:
C#中利用GDI+繪制旋轉(zhuǎn)文本的文字,網(wǎng)上有很多資料,基本都使用矩陣旋轉(zhuǎn)的方式實(shí)現(xiàn)。但基本都只提及按點(diǎn)旋轉(zhuǎn),若要實(shí)現(xiàn)在矩形范圍內(nèi)旋轉(zhuǎn)文本,資料較少。經(jīng)過(guò)琢磨,可以將矩形內(nèi)旋轉(zhuǎn)轉(zhuǎn)化為按點(diǎn)旋轉(zhuǎn),不過(guò)需要經(jīng)過(guò)不少的計(jì)算過(guò)程。利用下面的類可以實(shí)現(xiàn)該功能。
具體實(shí)現(xiàn)代碼如下:
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>
/// 繪制根據(jù)矩形旋轉(zhuǎn)文本
/// </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);
// 根據(jù)旋轉(zhuǎn)角度,求取旋轉(zhuǎn)后字符串大小
SizeF sizeRotate = ConvertSize(size, angle);
// 根據(jù)旋轉(zhuǎn)后尺寸、布局矩形、布局方式計(jì)算文本旋轉(zhuǎn)點(diǎn)
PointF rotatePt = GetRotatePoint(sizeRotate, layoutRectangle, format);
// 重設(shè)布局方式都為Center
StringFormat newFormat = new StringFormat(format);
newFormat.Alignment = StringAlignment.Center;
newFormat.LineAlignment = StringAlignment.Center;
// 繪制旋轉(zhuǎn)后文本
DrawString(s, font, brush, rotatePt, newFormat, angle);
}
/// <summary>
/// 繪制根據(jù)點(diǎn)旋轉(zhuǎn)文本,一般旋轉(zhuǎn)點(diǎn)給定位文本包圍盒中心點(diǎn)
/// </summary>
/// <param name="s">文本</param>
/// <param name="font">字體</param>
/// <param name="brush">填充</param>
/// <param name="point">旋轉(zhuǎn)點(diǎn)</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);
// 旋轉(zhuǎn)矩形四個(gè)頂點(diǎn)
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);
// 求取四個(gè)頂點(diǎn)的包圍盒
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;
}
}
}
測(cè)試代碼如下:
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;
// 繪制圍繞點(diǎn)旋轉(zhuǎn)的文本
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);
// 繪制矩形內(nèi)旋轉(zhuǎn)的文本
// 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);
}
}
}
效果如下圖:

完整實(shí)例點(diǎn)此本站下載。
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助
相關(guān)文章
C#使用SqlBulkCopy批量復(fù)制數(shù)據(jù)到數(shù)據(jù)表
這篇文章主要介紹了C#使用SqlBulkCopy批量復(fù)制數(shù)據(jù)到數(shù)據(jù)表的方法,較為詳細(xì)的講述了SqlBulkCopy批量復(fù)制數(shù)據(jù)到數(shù)據(jù)表的原理與實(shí)現(xiàn)技巧,需要的朋友可以參考下2014-10-10
C#開(kāi)發(fā)微信門(mén)戶及應(yīng)用(3) 文本消息和圖文消息應(yīng)答
這篇文章主要為大家詳細(xì)介紹了C#開(kāi)發(fā)微信門(mén)戶及應(yīng)用第二篇,微信文本消息和圖文消息的應(yīng)答,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
C#調(diào)用sql2000存儲(chǔ)過(guò)程方法小結(jié)
這篇文章主要介紹了C#調(diào)用sql2000存儲(chǔ)過(guò)程的方法,以實(shí)例形式分別對(duì)調(diào)用帶輸入?yún)?shù)及輸出參數(shù)的存儲(chǔ)過(guò)程進(jìn)行了詳細(xì)分析,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10
C#基礎(chǔ)學(xué)習(xí)系列之Attribute和反射詳解
大家在使用Attribute的時(shí)候大多需要用到反射,所以放在一起。下面這篇文章主要給大家介紹了關(guān)于C#基礎(chǔ)學(xué)習(xí)系列之Attribute和反射的相關(guān)資料,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
使用C#發(fā)送Http請(qǐng)求實(shí)現(xiàn)模擬登陸實(shí)例
本文主要介紹了使用C#發(fā)送Http請(qǐng)求實(shí)現(xiàn)模擬登陸實(shí)例,模擬登陸的原理簡(jiǎn)單,想要了解的朋友可以了解一下。2016-10-10
WPF實(shí)現(xiàn)動(dòng)畫(huà)效果(六)之路徑動(dòng)畫(huà)
這篇文章介紹了WPF實(shí)現(xiàn)動(dòng)畫(huà)效果之路徑動(dòng)畫(huà),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
VSCode調(diào)試C#程序及附缺失.dll文件的解決辦法
這篇文章主要介紹了VSCode調(diào)試C#程序及附缺失.dll文件的解決辦法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

