C#利用GDI+绘制旋转文字等效果实例
程序员文章站
2023-12-19 19:54:28
本文实例讲述了c#利用gdi+绘制旋转文字等效果的方法,是非常实用的技巧。分享给大家供大家参考之用。具体如下:
c#中利用gdi+绘制旋转文本的文字,网上有很多资料,基本...
本文实例讲述了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); } } }
效果如下图:
完整实例点此本站下载。
希望本文所述对大家的c#程序设计有所帮助