加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#利用GDI+绘制旋转文字等效果实例

发布时间:2020-12-15 05:43:07 所属栏目:百科 来源:网络整理
导读:本篇章节讲解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,PointF point,float angle) 
    { 
      // Save the matrix 
      Matrix mtxSave = _graphics.Transform; 
 
      Matrix mtxRotate = _graphics.Transform; 
      mtxRotate.RotateAt(angle,point); 
      _graphics.Transform = mtxRotate; 
 
      _graphics.DrawString(s,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,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,new PointF(200,-45f); 
      graphicsText.DrawString(_text,new PointF(300,90f); 
      graphicsText.DrawString(_text,new PointF(400,-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,rect,30); 
 
      rect.Location += new SizeF(180,0); 
      format.LineAlignment = StringAlignment.Near; 
      e.Graphics.DrawRectangle(_pen,-30); 
 
      rect.Location += new SizeF(180,0); 
      format.LineAlignment = StringAlignment.Center; 
      e.Graphics.DrawRectangle(_pen,-90); 
 
      rect.Location += new SizeF(180,0); 
      format.LineAlignment = StringAlignment.Far; 
      e.Graphics.DrawRectangle(_pen,70); 
 
      // Second line 
      rect = rc; 
      rect.Location += new SizeF(0,100); 
      format.Alignment = StringAlignment.Center; 
 
      e.Graphics.DrawRectangle(_pen,40); 
 
      rect.Location += new SizeF(180,-70); 
 
      rect.Location += new SizeF(180,60); 
 
      // Third line 
      rect = rc; 
      rect.Location += new SizeF(0,200); 
      format.Alignment = StringAlignment.Far; 
 
      e.Graphics.DrawRectangle(_pen,90); 
 
      rect.Location += new SizeF(180,45); 
    } 
  } 
} 

效果如下图:

完整实例点此本站下载。

希望本文所述对大家的C#程序设计有所帮助

您可能感兴趣的文章:

  • 深入c# GDI+简单绘图的具体操作步骤(一)
  • 深入c# GDI+简单绘图的具体操作步骤(三)
  • 深入c# GDI+简单绘图的具体操作步骤(四)
  • BarCode条形码基于C# GDI+ 的实现方法详解
  • 深入c# GDI+简单绘图的具体操作步骤(二)
  • C#常用GDI+文字操作汇总
  • C#使用GDI+创建缩略图实例
  • C# 使用 GDI+ 实现添加中心旋转(任意角度)的文字
  • C#利用GDI+画图的基础实例教程
  • C#利用GDI+给图片添加文字(文字自适应矩形区域)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读