如何使用C#精确测量长度文本绘制区域

9757人阅读
& & & & C#中利用GDI+绘制旋转文本的文字,网上有很多资料,基本都使用矩阵旋转的方式实现。但基本都只提及按点旋转,若要实现在矩形范围内旋转文本,资料较少。经过琢磨,可以将矩形内旋转转化为按点旋转,不过需要经过不少的计算过程。利用下面的类可以实现该功能。
using System.Collections.G
using System.D
using System.Drawing.Drawing2D;
namespace RotateText
public class GraphicsText
private Graphics _
public GraphicsText()
public Graphics Graphics
get { return _ }
set { _graphics = }
/// &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.C
newFormat.LineAlignment = StringAlignment.C
// 绘制旋转后文本
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.T
Matrix mtxRotate = _graphics.T
mtxRotate.RotateAt(angle, point);
_graphics.Transform = mtxR
_graphics.DrawString(s, font, brush, point, format);
// Reset the matrix
_graphics.Transform = mtxS
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.MaxV
float right = float.MinV
float top = float.MaxV
float bottom = float.MinV
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);
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;
case StringAlignment.Center:
pt.X = (layoutRectangle.Left + layoutRectangle.Right) / 2f;
case StringAlignment.Far:
pt.X = layoutRectangle.Right - size.Width / 2f;
switch (format.LineAlignment)
case StringAlignment.Near:
pt.Y = layoutRectangle.Top + size.Height / 2f;
case StringAlignment.Center:
pt.Y = (layoutRectangle.Top + layoutRectangle.Bottom) / 2f;
case StringAlignment.Far:
pt.Y = layoutRectangle.Bottom - size.Height / 2f;
测试代码如下:
using System.Collections.G
using System.D
using System.D
using System.Windows.F
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.G
// 绘制围绕点旋转的文本
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.C
format.LineAlignment = StringAlignment.C
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 =
format.Alignment = StringAlignment.N
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.N
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.C
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.F
e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
graphicsText.DrawString(_text, _font, _brush, rect, format, 70);
// Second line
rect.Location += new SizeF(0, 100);
format.Alignment = StringAlignment.C
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.N
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.C
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.F
e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
graphicsText.DrawString(_text, _font, _brush, rect, format, 60);
// Third line
rect.Location += new SizeF(0, 200);
format.Alignment = StringAlignment.F
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.N
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.C
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.F
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
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:317824次
积分:3768
积分:3768
排名:第8413名
原创:55篇
转载:65篇
评论:34条
(2)(7)(3)(4)(2)(1)(3)(2)(1)(2)(8)(8)(1)(2)(2)(1)(1)(1)(3)(4)(1)(2)(3)(3)(3)(6)(5)(2)(4)(3)(14)(10)(6)C#实现在图像中绘制文字图形的方法
作者:zhuzhao
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了C#实现在图像中绘制文字图形的方法,涉及绘图及图像的相关操作技巧,需要的朋友可以参考下
本文实例讲述了C#实现在图像中绘制文字图形的方法。分享给大家供大家参考。具体实现方法如下:
using System.Collections.G
using System.D
using System.D
using System.T
using System.Windows.F
using System.Drawing.Drawing2D;
using System.Drawing.I
namespace WindowsApplication2
public partial class Form20 : Form
public Form20()
InitializeComponent();
private void Form20_Load(object sender, EventArgs e)
private void button1_Click(object sender, EventArgs e)
Rectangle rect = new Rectangle(0, 0, 100, 100);
Graphics dg = this.CreateGraphics();
Image image = new Bitmap(rect.Width, rect.Height, dg);
Graphics ig = Graphics.FromImage(image);
ig.FillRectangle(Brushes.Black, rect);
Font font = new Font("Arial", 12);
Brush brush = System.Drawing.Brushes.W
ig.DrawString("zhuzhao", font, brush,0, 0);
//ImageFormat iformat=new ImageFormat(new Guid(Bmp));
image.Save(@"c:/image.png");
dg.DrawImage(image,new PointF(100,100));
希望本文所述对大家的C#程序设计有所帮助。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具2009年9月 .NET技术大版内专家分月排行榜第三2005年9月 .NET技术大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。网站已改版,请使用新地址访问:
ColorSkew C#编写的文本图像倾斜角度检测并 正的程序,输入一张倾 彩色 出 Special Effects 图形 处理 265万源代码下载-
&文件名称: ColorSkew& & [
& & & & &&]
&&所属分类:
&&开发工具: C#
&&文件大小: 2332 KB
&&上传时间:
&&下载次数: 102
&&提 供 者:
&详细说明:C#编写的文本图像倾斜角度检测并且校正的程序,输入一张倾斜的彩色文本图像,输出角度校正之后的二值化图像。包含测试图片。-C# write text image tilt Angle detection and correction program, input a tilted color text image, output Angle after correction, binary image. Contains test images.
文件列表(点击判断是否您需要的文件,如果是垃圾请在下面评价投诉):
&&ColorSkew\ColorSkew\bin\Debug\ColorSkew.exe&&.........\.........\...\.....\ColorSkew.pdb&&.........\.........\...\.....\ColorSkew.vshost.exe&&.........\.........\...\.....\ColorSkew.vshost.exe.manifest&&.........\.........\...\.....\mpie.dll&&.........\.........\...\.....\mpli.dll&&.........\.........\...\Release\mpli.dll&&.........\.........\BitmapProcess.cs&&.........\.........\bwimage1.bmp&&.........\.........\bwimage2.bmp&&.........\.........\BW_image1.tif&&.........\.........\colorimage.bmp&&.........\.........\ColorSkew.csproj&&.........\.........\colorskew1.bmp&&.........\.........\colorskew3.bmp&&.........\.........\GATOSBWimage.bmp&&.........\.........\mpie.dll&&.........\.........\mpli.dll&&.........\.........\MP_DotNetImageIO.cs&&.........\.........\MP_DotNetLine.cs&&.........\.........\MP_Library.cs&&.........\.........\obj\x86\Debug\ColorSkew.csproj.FileListAbsolute.txt&&.........\.........\...\...\.....\ColorSkew.exe&&.........\.........\...\...\.....\ColorSkew.pdb&&.........\.........\...\...\.....\DesignTimeResolveAssemblyReferencesInput.cache&&.........\.........\...\...\.....\ResolveAssemblyReference.cache&&.........\.........\Program.cs&&.........\.........\...perties\AssemblyInfo.cs&&.........\.........\企业法人营业执照.jpg&&.........\ColorSkew.sln&&.........\ColorSkew.suo&&.........\.........\obj\x86\Debug\TempPE&&.........\.........\...\...\Debug&&.........\.........\bin\Debug&&.........\.........\...\Release&&.........\.........\obj\x86&&.........\.........\bin&&.........\.........\obj&&.........\.........\Properties&&.........\ColorSkew&&ColorSkew
&[]:纯粹是垃圾&[]:纯粹是垃圾
&近期下载过的用户:
&&&&&&&&&&&[]
&相关搜索:
&输入关键字,在本站265万海量源码库中尽情搜索:
&[] - 完成在复杂背景下的文字分割,如多色彩文字,倾斜文字,环形文字等
&[] - 校正扫倾斜的文档图像,绝对可用,国外新作
&[] - 文档倾斜校正,使用霍夫变换来处理的数字化图像处理
&[] - C#编写的边缘检测程序,集合了常用的边缘检测算法,高斯,形态学,小波,Canny 等等。}

我要回帖

更多关于 精确测量 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信