C# Graphics 绘制平滑字体/高质量字体

在静态类里添加静态方法

public static void DrawSmoothString(this Graphics graphics, string content, Font font, Brush brush, float x, float y)
{
    float emSize = graphics.DpiY * font.Size / 72;
    using var path = new GraphicsPath();
    path.AddString(content, font.FontFamily, (int)font.Style, emSize, new PointF(x, y), StringFormat.GenericDefault);
    //SmoothingMode must be set, or text smoothing will not work
    graphics.SmoothingMode = SmoothingMode.HighQuality;
    graphics.FillPath(brush, path);
}

使用时

var font = new Font("Microsoft Sans Serif", 25);
var brush = new SolidBrush(Color.Black);
graphics.DrawSmoothString("", font , brush , 0, 20);
posted @ 2024-05-09 23:58  逍遥子k  阅读(113)  评论(0编辑  收藏  举报