From dc4d7a4840cd47da025b9ca4402921ec3bf95103 Mon Sep 17 00:00:00 2001 From: Sunny Date: Wed, 25 Oct 2023 11:23:50 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E5=AD=97=E4=BD=93=E5=9B=BE?= =?UTF-8?q?=E6=A0=87=E6=98=BE=E7=A4=BA=EF=BC=8C=E4=BB=A5=E6=9C=9F=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=AD=97=E4=BD=93=E5=9B=BE=E6=A0=87=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E5=8F=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SunnyUI/Controls/UINavMenu.cs | 5 ++- SunnyUI/Controls/UISymbolLabel.cs | 2 +- SunnyUI/Font/UFontImageHelper.cs | 53 +++++++++++++++++++------------ 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/SunnyUI/Controls/UINavMenu.cs b/SunnyUI/Controls/UINavMenu.cs index 94c354b4..0e08fd73 100644 --- a/SunnyUI/Controls/UINavMenu.cs +++ b/SunnyUI/Controls/UINavMenu.cs @@ -742,7 +742,10 @@ namespace Sunny.UI int size = 24; int left = Width - size - 6; if (Bar.Visible) left -= Bar.Width; - e.Graphics.DrawFontImage(e.Node.IsExpanded ? 61702 : 61703, 24, ForeColor, left, e.Bounds.Y + (ItemHeight - 24) / 2); + + SizeF sf = e.Graphics.GetFontImageSize(61702, 24); + Rectangle rect = new Rectangle((int)(left + sf.Width / 2) - 12, e.Bounds.Y, 24, e.Bounds.Height); + e.Graphics.DrawFontImage(e.Node.IsExpanded ? 61702 : 61703, 24, ForeColor, rect); } //鏄剧ずTips鍦嗗湀 diff --git a/SunnyUI/Controls/UISymbolLabel.cs b/SunnyUI/Controls/UISymbolLabel.cs index 98272cb2..48afa469 100644 --- a/SunnyUI/Controls/UISymbolLabel.cs +++ b/SunnyUI/Controls/UISymbolLabel.cs @@ -289,7 +289,7 @@ namespace Sunny.UI } if (Text.IsNullOrEmpty()) - e.Graphics.DrawFontImage(Symbol, SymbolSize, symbolColor, (Width - SymbolSize) / 2.0f, (Height - SymbolSize) / 2.0f, SymbolOffset.X, SymbolOffset.Y); + e.Graphics.DrawFontImage(Symbol, SymbolSize, symbolColor, new RectangleF((Width - SymbolSize) / 2.0f, (Height - SymbolSize) / 2.0f, SymbolSize, SymbolSize), SymbolOffset.X, SymbolOffset.Y); else e.Graphics.DrawFontImage(Symbol, SymbolSize, symbolColor, new Rectangle(rect.Left, rect.Top, SymbolSize, rect.Height), SymbolOffset.X, SymbolOffset.Y); diff --git a/SunnyUI/Font/UFontImageHelper.cs b/SunnyUI/Font/UFontImageHelper.cs index 23a7fae5..8db19896 100644 --- a/SunnyUI/Font/UFontImageHelper.cs +++ b/SunnyUI/Font/UFontImageHelper.cs @@ -74,7 +74,7 @@ namespace Sunny.UI /// 字符 /// 大小 /// 字体大小 - private static SizeF GetFontImageSize(this Graphics graphics, int symbol, int symbolSize) + internal static SizeF GetFontImageSize(this Graphics graphics, int symbol, int symbolSize) { Font font = GetFont(symbol, symbolSize); if (font == null) @@ -109,32 +109,43 @@ namespace Sunny.UI RectangleF rect, int xOffset = 0, int yOffSet = 0, int angle = 0) { SizeF sf = graphics.GetFontImageSize(symbol, symbolSize); - - if (angle == 0) - { - graphics.DrawFontImage(symbol, symbolSize, color, rect.Left + ((rect.Width - sf.Width) / 2.0f).RoundEx(), - rect.Top + ((rect.Height - sf.Height) / 2.0f).RoundEx(), xOffset, yOffSet); - } - else - { - graphics.DrawFontImage(symbol, symbolSize, color, angle, rect, xOffset, yOffSet); - } + graphics.DrawFontImage(symbol, symbolSize, color, rect.Left + ((rect.Width - sf.Width) / 2.0f).RoundEx(), + rect.Top + ((rect.Height - sf.Height) / 2.0f).RoundEx() + 1, xOffset, yOffSet); } public static void DrawFontImage(this Graphics graphics, int symbol, int symbolSize, Color color, int angle, RectangleF rect, int xOffset = 0, int yOffSet = 0) { SizeF sf = graphics.GetFontImageSize(symbol, symbolSize); - PointF center = rect.Center(); - Font font = GetFont(symbol, symbolSize); + using Bitmap bmp = new Bitmap(symbolSize, symbolSize); + using Graphics g = Graphics.FromImage(bmp); + g.DrawFontImage(symbol, symbolSize, color, (symbolSize - sf.Width) / 2.0f, (symbolSize - sf.Height) / 2.0f); - var symbolValue = GetSymbolValue(symbol); - string text = char.ConvertFromUtf32(symbolValue); - graphics.TextRenderingHint = TextRenderingHint.AntiAlias; - graphics.InterpolationMode = InterpolationMode.HighQualityBilinear; - graphics.DrawRotateString(text, font, color, center, angle, xOffset, yOffSet); - graphics.TextRenderingHint = TextRenderingHint.SystemDefault; - graphics.InterpolationMode = InterpolationMode.Default; + using Bitmap bmp1 = RotateTransform(bmp, angle); + graphics.DrawImage(angle == 0 ? bmp : bmp1, + new RectangleF(rect.Left + (rect.Width - bmp1.Width) / 2.0f, rect.Top + (rect.Height - bmp1.Height) / 2.0f, bmp1.Width, bmp1.Height), + new RectangleF(0, 0, bmp1.Width, bmp1.Height), + GraphicsUnit.Pixel); + } + + private static Bitmap RotateTransform(Bitmap originalImage, int angle) + { + // 创建新的位图,大小为旋转后的图片大小 + Bitmap rotatedImage = new Bitmap(originalImage.Height, originalImage.Width); + + // 创建绘图对象 + using Graphics g = Graphics.FromImage(rotatedImage); + + // 设置绘图参数,将原始图片旋转90° + g.TranslateTransform(originalImage.Width / 2, originalImage.Height / 2); + g.RotateTransform(angle); + + g.DrawImage(originalImage, + new Rectangle(-originalImage.Width / 2, -originalImage.Height / 2, originalImage.Width, originalImage.Height), + new Rectangle(0, 0, rotatedImage.Width, originalImage.Height), + GraphicsUnit.Pixel); + g.TranslateTransform(-originalImage.Width / 2, -originalImage.Height / 2); + return rotatedImage; } /// @@ -148,7 +159,7 @@ namespace Sunny.UI /// 上 /// 左右偏移 /// 上下偏移 - public static void DrawFontImage(this Graphics graphics, int symbol, int symbolSize, Color color, + private static void DrawFontImage(this Graphics graphics, int symbol, int symbolSize, Color color, float left, float top, int xOffset = 0, int yOffSet = 0) { Font font = GetFont(symbol, symbolSize);