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);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章