Using GDI+, what's the easiest approach to align text (drawn in several different fonts) along a common baseline?

北慕城南 提交于 2019-12-03 14:10:33
hashi

I thinks this way is work , please you try.

List<RectangleF> rects = new List<RectangleF>();

private void Form1_Paint(object sender, PaintEventArgs e)
{
    ////////////////////Not Set baseLine
    //baseline
    e.Graphics.DrawLine(Pens.Red , new Point(100,200),new Point(800,200));

    //words
    Point point = new Point(100,100);
    e.Graphics.DrawString("hello world", new Font("Times", 30), Brushes.Black, point);
    RectangleF rectangleF = new RectangleF(point, e.Graphics.MeasureString("hello world", new Font("Times", 30)));
    e.Graphics.DrawRectangle(Pens.Green,rectangleF.X ,rectangleF.Y , rectangleF.Width , rectangleF.Height);
    rects.Add(rectangleF);

    point = new Point(400, 100);
    e.Graphics.DrawString("hello world", new Font("Arial", 40), Brushes.Black, point);
    rectangleF = new RectangleF(point, e.Graphics.MeasureString("hello world", new Font("Arial", 40)));
    e.Graphics.DrawRectangle(Pens.Green, rectangleF.X, rectangleF.Y, rectangleF.Width, rectangleF.Height);
    rects.Add(rectangleF);

    point = new Point(800, 100);
    e.Graphics.DrawString("hello world", new Font("Courier", 20), Brushes.Black, point);
    rectangleF = new RectangleF(point, e.Graphics.MeasureString("hello world", new Font("Courier", 20)));
    e.Graphics.DrawRectangle(Pens.Green, rectangleF.X, rectangleF.Y, rectangleF.Width, rectangleF.Height);
    rects.Add(rectangleF);

    ///////////////////SetBaseLine/////////////////////////////
    var maxHeight = GetMaxHeight();
    ///////////////////

    //baseLine
    e.Graphics.DrawLine(Pens.Pink, new Point(100, (int) (400 + maxHeight / 2)), new Point(800, (int) (400 + maxHeight / 2)));

    StringFormat stringFormat = new StringFormat();
    stringFormat.LineAlignment = StringAlignment.Center;

    //words
    point = new Point(100, 400);
    rectangleF = new RectangleF(point, e.Graphics.MeasureString("hello world", new Font("Times", 30)));
    e.Graphics.DrawString("hello world", new Font("Times", 30), Brushes.Black, new RectangleF(rectangleF.X ,rectangleF.Y , rectangleF.Width , maxHeight) , stringFormat);
    e.Graphics.DrawRectangle(Pens.Green, rectangleF.X, rectangleF.Y, rectangleF.Width, rectangleF.Height);
    rects.Add(rectangleF);

    point = new Point(400, 400);
    rectangleF = new RectangleF(point, e.Graphics.MeasureString("hello world", new Font("Arial", 40)));
    e.Graphics.DrawString("hello world", new Font("Arial", 40), Brushes.Black, new RectangleF(rectangleF.X, rectangleF.Y, rectangleF.Width, maxHeight), stringFormat);
    e.Graphics.DrawRectangle(Pens.Green, rectangleF.X, rectangleF.Y, rectangleF.Width, rectangleF.Height);
    rects.Add(rectangleF);

    point = new Point(800, 400);
    rectangleF = new RectangleF(point, e.Graphics.MeasureString("hello world", new Font("Courier", 20)));
    e.Graphics.DrawString("hello world", new Font("Courier", 20), Brushes.Black, new RectangleF(rectangleF.X, rectangleF.Y, rectangleF.Width, maxHeight), stringFormat);
    e.Graphics.DrawRectangle(Pens.Green, rectangleF.X, rectangleF.Y, rectangleF.Width, rectangleF.Height);
    rects.Add(rectangleF);

}

private float GetMaxHeight()
{
    float temp = 0;
    foreach (RectangleF rectangleF in rects)
        if (rectangleF.Height > temp)
            temp = rectangleF.Height;

    return temp;
}

I've been researching the same thing for the last few days, and I finally found an answer on this blog page. This code (at the bottom of the article) worked really well for me and hopefully helps anyone else struggling with this problem:

private void DrawOnBaseline(string s, Graphics g, Font f, Brush b, Point pos)
    {
      float baselineOffset=f.SizeInPoints/f.FontFamily.GetEmHeight(f.Style)*f.FontFamily.GetCellAscent(f.Style);
      float baselineOffsetPixels = g.DpiY/72f*baselineOffset;

      g.DrawString(s,f,b,new Point(pos.X,pos.Y-(int)(baselineOffsetPixels+0.5f)),StringFormat.GenericTypographic);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!