* 重构DrawString函数

This commit is contained in:
Sunny 2023-05-16 10:37:48 +08:00
parent 35b00ce5c2
commit 90c26a6b24

View File

@ -17,12 +17,14 @@
* : 2022-06-11 * : 2022-06-11
* *
* 2022-06-11: V3.1.9 * 2022-06-11: V3.1.9
* 2022-05-16: V3.3.6 DrawString函数
******************************************************************************/ ******************************************************************************/
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using System.Drawing; using System.Drawing;
using System.Drawing.Drawing2D; using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace Sunny.UI namespace Sunny.UI
{ {
@ -102,9 +104,9 @@ namespace Sunny.UI
/// <param name="code">验证码表达式</param> /// <param name="code">验证码表达式</param>
private Bitmap CreateImage(string code) private Bitmap CreateImage(string code)
{ {
Font font = new Font(Font.Name, CodeFontSize); Font font = new Font(Font.Name, CodeFontSize, FontStyle.Bold);
Code = code; Code = code;
SizeF sf = GDI.MeasureString(code, font); Size sf = TextRenderer.MeasureText(code, font);
Bitmap image = new Bitmap((int)sf.Width + 16, Height - 2); Bitmap image = new Bitmap((int)sf.Width + 16, Height - 2);
@ -134,7 +136,8 @@ namespace Sunny.UI
image.SetPixel(x, y, Color.FromArgb(random.Next())); image.SetPixel(x, y, Color.FromArgb(random.Next()));
} }
g.DrawString(code, font, rectColor, image.Width / 2 - sf.Width / 2, image.Height / 2 - sf.Height / 2); using Brush br = new SolidBrush(rectColor);
g.DrawString(code, font, br, image.Width / 2 - sf.Width / 2, image.Height / 2 - sf.Height / 2);
var imageex = TwistImage(image, true, 5, 5); var imageex = TwistImage(image, true, 5, 5);
font.Dispose(); font.Dispose();
image.Dispose(); image.Dispose();