C# 使用 GDI+ 實(shí)現(xiàn)添加中心旋轉(zhuǎn)(任意角度)的文字
前言
這篇文章是 GDI+ 總結(jié)系列的第三篇,如果對 GDI+ 的基礎(chǔ)使用不熟悉的朋友可以先看第一篇文章《C# 使用 GDI+ 畫圖》。
需求
需求是要實(shí)現(xiàn)給圖片添加任意角度旋轉(zhuǎn)的文字,文字的旋轉(zhuǎn)中心要是在文字區(qū)域中央,就像 CSS 的 rotate 函數(shù)一樣的效果。如下:
分析&思路
Graphics 類有個(gè) RotateTransform 方法,可以傳入任意角度的值來旋轉(zhuǎn)畫板。但是這個(gè)方法的旋轉(zhuǎn)中心是畫板的左上角,所以直接單單用這個(gè)方法不能滿足我們的需求。此外, Graphics 類還有個(gè) TranslateTransform 方法可以改變坐標(biāo)的原點(diǎn),而且這個(gè)方法是沿著矩形的x,y軸平移的,即就算圖片旋轉(zhuǎn)了一定的角度后,再調(diào)用 TranslateTransform 方法,它還是沿著x,y軸平移。于是通過以下三個(gè)步驟即可實(shí)現(xiàn)圖片中心旋轉(zhuǎn)。
- 把畫板(Graphics對象)原點(diǎn)平移到矩形中心位置(x, y)
- 在(x, y)位置繞原點(diǎn)旋轉(zhuǎn)畫板N度
- 畫板退回(-x, -y)的距離
還是看不懂的同學(xué)看下面的圖應(yīng)該就明白了
明白了原理,那不容易推斷出,如果要旋轉(zhuǎn)的中心不是圖片中心而是文字中心,那步驟還是一樣的,只是把(x, y)改為文字中心的坐標(biāo)就好了。
除了上面說的方法,其實(shí)還有一個(gè)方法可以實(shí)現(xiàn)中心旋轉(zhuǎn),那就是使用 Matrix 類。 Matrix 類的 RotateAt 方法可以指定矩陣旋轉(zhuǎn)的中心位置。
// // 摘要: // 沿 point 參數(shù)中指定的點(diǎn)并通過預(yù)先計(jì)算該旋轉(zhuǎn),來順時(shí)針旋轉(zhuǎn)此 System.Drawing.Drawing2D.Matrix。 // // 參數(shù): // angle: // 旋轉(zhuǎn)角度(范圍)(單位:度)。 // // point: // 一個(gè) System.Drawing.PointF,表示旋轉(zhuǎn)中心。 [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public void RotateAt(float angle, PointF point);
Graphics 類的 Transform 屬性返回的就是 Matrix 對象,該屬性可以 get 、 set 。因此我們先獲取原來的畫板的矩陣,然后使用 RotateAt 方法旋轉(zhuǎn)該矩陣,再把旋轉(zhuǎn)后的矩陣賦值給畫板就好了。
具體實(shí)現(xiàn)
添加任意角度文字方法
/// <summary> /// 圖片添加任意角度文字(文字旋轉(zhuǎn)是中心旋轉(zhuǎn),角度順時(shí)針為正) /// </summary> /// <param name="imgPath">圖片路徑</param> /// <param name="locationLeftTop">文字左上角定位(x1,y1)</param> /// <param name="fontSize">字體大小,單位為像素</param> /// <param name="text">文字內(nèi)容</param> /// <param name="angle">文字旋轉(zhuǎn)角度</param> /// <param name="fontName">字體名稱</param> /// <returns>添加文字后的Bitmap對象</returns> public Bitmap AddText(string imgPath, string locationLeftTop, int fontSize, string text, int angle = 0, string fontName = "華文行楷") { Image img = Image.FromFile(imgPath); int width = img.Width; int height = img.Height; Bitmap bmp = new Bitmap(width, height); Graphics graphics = Graphics.FromImage(bmp); // 畫底圖 graphics.DrawImage(img, 0, 0, width, height); Font font = new Font(fontName, fontSize, GraphicsUnit.Pixel); SizeF sf = graphics.MeasureString(text, font); // 計(jì)算出來文字所占矩形區(qū)域 // 左上角定位 string[] location = locationLeftTop.Split(','); float x1 = float.Parse(location[0]); float y1 = float.Parse(location[1]); // 進(jìn)行文字旋轉(zhuǎn)的角度定位 if (angle != 0) { #region 法一:TranslateTransform平移 + RotateTransform旋轉(zhuǎn) /* * 注意: * Graphics.RotateTransform的旋轉(zhuǎn)是以Graphics對象的左上角為原點(diǎn),旋轉(zhuǎn)整個(gè)畫板的。 * 同時(shí)x,y坐標(biāo)軸也會跟著旋轉(zhuǎn)。即旋轉(zhuǎn)后的x,y軸依然與矩形的邊平行 * 而Graphics.TranslateTransform方法,是沿著x,y軸平移的 * 因此分三步可以實(shí)現(xiàn)中心旋轉(zhuǎn) * 1.把畫板(Graphics對象)平移到旋轉(zhuǎn)中心 * 2.旋轉(zhuǎn)畫板 * 3.把畫板平移退回相同的距離(此時(shí)的x,y軸仍然是與旋轉(zhuǎn)后的矩形平行的) */ //// 把畫板的原點(diǎn)(默認(rèn)是左上角)定位移到文字中心 //graphics.TranslateTransform(x1 + sf.Width / 2, y1 + sf.Height / 2); //// 旋轉(zhuǎn)畫板 //graphics.RotateTransform(angle); //// 回退畫板x,y軸移動過的距離 //graphics.TranslateTransform(-(x1 + sf.Width / 2), -(y1 + sf.Height / 2)); #endregion #region 法二:矩陣旋轉(zhuǎn) Matrix matrix = graphics.Transform; matrix.RotateAt(angle, new PointF(x1 + sf.Width / 2, y1 + sf.Height / 2)); graphics.Transform = matrix; #endregion } // 寫上自定義角度的文字 graphics.DrawString(text, font, new SolidBrush(Color.Black), x1, y1); graphics.Dispose(); img.Dispose(); return bmp; }
PS:這里簡單解釋一下為什么文字中心是 (x1 + sf.Width / 2, y1 + sf.Height / 2) ,因?yàn)?(x, y) 是左上角,而 sf.Width 、 sf.Height 是文字矩形區(qū)域?qū)?、高。如圖:
測試調(diào)用
private static void Main(string[] args) { try { Console.WriteLine("Start drawing ..."); DrawingEntity drawing = new DrawingEntity(); System.Drawing.Bitmap bmp = drawing.AddText(@"D:\test\1.png", "176.94,150.48", 66, "寫點(diǎn)啥好呢", 30); bmp.Save(@"D:\test\output.png"); bmp.Dispose(); Console.WriteLine("Done!"); } catch (System.Exception ex) { Console.WriteLine(ex.ToString()); } finally { System.Console.WriteLine("\nPress any key to continue ..."); System.Console.ReadKey(); } }
最終效果
沒有旋轉(zhuǎn)時(shí)
中心旋轉(zhuǎn)30度
總結(jié)
以上所述是小編給大家介紹的C# 使用 GDI+ 實(shí)現(xiàn)添加中心旋轉(zhuǎn)(任意角度)的文字,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- c# WinForm制作圖片編輯工具(圖像拖動、縮放、旋轉(zhuǎn)、摳圖)
- C#使用opencv截取旋轉(zhuǎn)矩形區(qū)域圖像的實(shí)現(xiàn)示例
- 利用C#代碼實(shí)現(xiàn)圖片旋轉(zhuǎn)360度
- C#實(shí)現(xiàn)計(jì)算一個(gè)點(diǎn)圍繞另一個(gè)點(diǎn)旋轉(zhuǎn)指定弧度后坐標(biāo)值的方法
- C#中圖片旋轉(zhuǎn)和翻轉(zhuǎn)(RotateFlipType)用法分析
- C#控制圖像旋轉(zhuǎn)和翻轉(zhuǎn)的方法
- C# VTK 移動旋轉(zhuǎn)交互功能實(shí)現(xiàn)
相關(guān)文章
C#實(shí)現(xiàn)一個(gè)簡單實(shí)用的TXT文本操作及日志框架詳解
這篇文章主要給大家介紹了關(guān)于利用C#如何實(shí)現(xiàn)一個(gè)簡單實(shí)用的TXT文本操作及日志框架的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們一起來看看吧2018-07-07C# 表達(dá)式目錄樹Expression的實(shí)現(xiàn)
本文主要介紹了C# 表達(dá)式目錄樹Expression的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09利用MySqlBulkLoader實(shí)現(xiàn)批量插入數(shù)據(jù)的示例詳解
MySQLBulkLoader是MySQL?Connector/Net類中的一個(gè)類,用于包裝MySQL語句。本文將利用MySqlBulkLoader實(shí)現(xiàn)批量插入數(shù)據(jù)功能,感興趣的可以了解一下2022-06-06