You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#中设置文本字号与行间距并生成图片?

实现C#多行文本统一字号+自定义小行间距的图片生成

嘿,既然你已经有基础的文本转图片经验,那搞定自定义行间距其实很简单——核心就是不要依赖GDI+默认的行高,手动控制每一行文本的绘制位置。下面我给你一套完整的实现方案,直接就能用:

核心思路

默认情况下,DrawString的行高是由字体本身决定的(一般是字号的1.2-1.5倍),但我们可以通过逐行绘制文本,手动计算每一行的Y坐标,把行间距精准控制在你想要的8个单位(这里建议用像素,直观好控制)。

完整代码示例

using System.Drawing;
using System.Drawing.Imaging;

public class TextToImageGenerator
{
    public static void GenerateMultiLineImage()
    {
        // 1. 准备多行文本内容
        string[] textLines = new[] { "第一行自定义文本", "第二行自定义文本", "第三行自定义文本" };
        
        // 2. 定义参数:字号24像素,行间距8像素,字体、颜色、边距
        int fontSize = 24;
        int lineSpacing = 8;
        string fontName = "微软雅黑";
        Color textColor = Color.Black;
        Color bgColor = Color.White;
        int padding = 10; // 图片内边距,避免文本贴边
        
        // 3. 创建字体(指定单位为像素,确保尺寸精准)
        using (Font font = new Font(fontName, fontSize, FontStyle.Regular, GraphicsUnit.Pixel))
        using (Brush textBrush = new SolidBrush(textColor))
        {
            // 4. 计算图片的宽高:取最长行的宽度,总高度=每行高度*行数 + 行间距*(行数-1) + 上下边距
            int maxLineWidth = 0;
            using (Bitmap tempBmp = new Bitmap(1, 1))
            using (Graphics tempGraphics = Graphics.FromImage(tempBmp))
            {
                foreach (string line in textLines)
                {
                    SizeF lineSize = tempGraphics.MeasureString(line, font);
                    if (lineSize.Width > maxLineWidth)
                    {
                        maxLineWidth = (int)Math.Ceiling(lineSize.Width);
                    }
                }
            }
            
            int totalHeight = padding * 2 + textLines.Length * font.Height + lineSpacing * (textLines.Length - 1);
            int totalWidth = padding * 2 + maxLineWidth;
            
            // 5. 创建图片和绘图对象
            using (Bitmap bmp = new Bitmap(totalWidth, totalHeight))
            using (Graphics graphics = Graphics.FromImage(bmp))
            {
                // 设置绘图质量(抗锯齿、文本渲染)
                graphics.Clear(bgColor);
                graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                
                // 6. 逐行绘制文本,手动控制Y坐标
                int currentY = padding; // 第一行的起始Y坐标(加上上边距)
                StringFormat format = new StringFormat()
                {
                    Alignment = StringAlignment.Near, // 左对齐,可改成Center居中
                    LineAlignment = StringAlignment.Center
                };
                
                foreach (string line in textLines)
                {
                    // 绘制当前行
                    graphics.DrawString(line, font, textBrush, new PointF(padding, currentY), format);
                    
                    // 更新下一行的Y坐标:当前行高度 + 自定义行间距
                    currentY += font.Height + lineSpacing;
                }
                
                // 7. 保存图片(可替换为其他格式:Jpeg、Png等)
                bmp.Save("多行文本图片.png", ImageFormat.Png);
            }
        }
    }
}

关键细节解释

  • 字体单位:代码里用GraphicsUnit.Pixel,这样字号24就是24像素,行间距8也是8像素,完全匹配你的需求。如果非要用磅(Point),可以把GraphicsUnit.Pixel改成GraphicsUnit.Point,但行间距的单位也要对应,不过像素更直观,推荐用这个。
  • 行间距控制:currentY += font.Height + lineSpacing这行是核心——font.Height是当前字体的总高度(从文本顶部到底部),加上你要的8像素间距,就是下一行的起始位置,保证两行之间的空白刚好是8像素。
  • 图片尺寸计算:先通过临时Graphics计算最长行的宽度,再结合行数、行间距和边距算出总高度,避免图片裁剪文本。
  • 绘图质量:开启抗锯齿和高质量文本渲染,让生成的图片更清晰。

额外调整建议

  • 如果要居中对齐文本,把StringAlignment.Near改成StringAlignment.Center即可。
  • 要是需要背景透明,把bgColor改成Color.Transparent,保存格式选Png(Jpeg不支持透明)。
  • 可以根据需求调整字体样式(加粗、斜体),修改FontStyle.Regular为FontStyle.Bold等。

内容的提问来源于stack exchange,提问作者Goober Grape

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:28:37