C#利用GDI+繪制旋轉文字等效果,
C#中利用GDI+繪制旋轉文本的文字,網上有很多資料,基本都使用矩陣旋轉的方式實現。但基本都只提及按點旋轉,若要實現在矩形范圍內旋轉文本,資料較少。經過琢磨,可以將矩形內旋轉轉化為按點旋轉,不過需要經過不少的計算過程。利用下面的類可以實現該功能。
[csharp] view plaincopy
- 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;
- }
- }
- }
測試代碼如下:
[csharp] view plaincopy
- 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:\
可以的
參考這個對C盤進行清理:
1.關閉系統還原:我的電腦屬性/系統還原/關閉所有磁盤上的系統還原,但是以後就不能用系統還原了!
2.關閉系統休眠:控制面板/電源/休眠/在啟動系統休眠前面的勾去掉
3.移動虛擬內存,我的電腦屬性/高級/性能/設置/高級/更改/選C盤也就是系統盤,選無分頁面,然後把虛擬內存設置到其磁盤,要剩余磁盤空間多的磁盤,比如D,E,F等盤. 設成內存的1.5~2.5倍,大小可設成一樣!
5.清理IE臨時文件夾,internet選項,刪除臨時文件和脫機文件
6.刪除系統日志和程序日志,我的電腦/控制面板/管理工具/計算機管理/事件查看器/應用程序,鼠標右鍵/清除所事件,在依次清除系統日志
7.清理系統緩存:2000系統是:C:\WINNT\system32\dllcache下的所有文件
XP系統是:C:\windows\system32\dllcache下的所有文件 清理系統緩存(打開我的電腦/工具/文件和文件夾選項/隱藏受保護的系統文件的勾去掉在把顯示全部文件勾上)。也可以直接運行sfc.exe /purgecache命令自動刪除。
8.清空回收站
9.刪除c:\windows\SoftwareDistribution\Download下的文件(系統更新時下載的文件如你裝好了更新也就沒有用了)
10.刪除c:\windows\RegisteredPackages下所有目錄
11.刪除C:\WINDOWS\Downloaded Program Files下所有的文件
12.我的電腦 文件夾選項 查看 隱藏已知受系統保護的文件勾去掉,顯示所有文件勾上確定。
13.刪除c:\windows\所有帶$8882305$的文件(系統更新後的備份文件)
zhidao.baidu.com/question/11035955.html
zhidao.baidu.com/question/12223613.html
zhidao.baidu.com/question/14874715.html
......余下全文>>
C:\
可以的
參考這個對C盤進行清理:
1.關閉系統還原:我的電腦屬性/系統還原/關閉所有磁盤上的系統還原,但是以後就不能用系統還原了!
2.關閉系統休眠:控制面板/電源/休眠/在啟動系統休眠前面的勾去掉
3.移動虛擬內存,我的電腦屬性/高級/性能/設置/高級/更改/選C盤也就是系統盤,選無分頁面,然後把虛擬內存設置到其磁盤,要剩余磁盤空間多的磁盤,比如D,E,F等盤. 設成內存的1.5~2.5倍,大小可設成一樣!
5.清理IE臨時文件夾,internet選項,刪除臨時文件和脫機文件
6.刪除系統日志和程序日志,我的電腦/控制面板/管理工具/計算機管理/事件查看器/應用程序,鼠標右鍵/清除所事件,在依次清除系統日志
7.清理系統緩存:2000系統是:C:\WINNT\system32\dllcache下的所有文件
XP系統是:C:\windows\system32\dllcache下的所有文件 清理系統緩存(打開我的電腦/工具/文件和文件夾選項/隱藏受保護的系統文件的勾去掉在把顯示全部文件勾上)。也可以直接運行sfc.exe /purgecache命令自動刪除。
8.清空回收站
9.刪除c:\windows\SoftwareDistribution\Download下的文件(系統更新時下載的文件如你裝好了更新也就沒有用了)
10.刪除c:\windows\RegisteredPackages下所有目錄
11.刪除C:\WINDOWS\Downloaded Program Files下所有的文件
12.我的電腦 文件夾選項 查看 隱藏已知受系統保護的文件勾去掉,顯示所有文件勾上確定。
13.刪除c:\windows\所有帶$8882305$的文件(系統更新後的備份文件)
zhidao.baidu.com/question/11035955.html
zhidao.baidu.com/question/12223613.html
zhidao.baidu.com/question/14874715.html
......余下全文>>