繁体   English   中英

Graphics.DrawString位于printdocument宽度的中心

[英]Graphics.DrawString center in printdocument width

我正在尝试在打印文档上将字符串居中。 我已经对图像进行了以下操作,它可以工作,但对于字符串似乎却不起作用。

这是我用来使图像居中的代码

e.Graphics.DrawImage(logo, (e.MarginBounds.Width / 2) - (logo.Width / 2), height);

我试图居中的文本是从TabControl中的Tab提供的

 using (var sf = new StringFormat())
 {
       height = logo.Height + 15;
       sf.LineAlignment = StringAlignment.Center;
       sf.Alignment = StringAlignment.Center;
       e.Graphics.DrawString(tabData.Text, new Font(this.Font.Name, 10),
            new SolidBrush(tabData.ForeColor),
            (e.MarginBounds.Width / 2) - (txtData.Width / 2), height, sf);
  }

我也在下面尝试过,并使用string_size.Width / 2代替txtData.Width

SizeF string_size = e.Graphics.MeasureString(tabData.Text, tabData.Font);

编辑

当前完整代码

        float height = 0;
        tabData.Text = "Date Range: 02/02/2010 - 08/09/2013"; //set just for testing
        using (var logo = Properties.Resources.title)
        {
            e.Graphics.DrawImage(logo, e.PageBounds.Left + (e.MarginBounds.Width / 2) - (logo.Width / 2), height);
            height = logo.Height + 15;
        }

        using (var sf = new StringFormat())
        {

            sf.LineAlignment = StringAlignment.Center;
            sf.Alignment = StringAlignment.Center;
            e.Graphics.DrawString(tabData.Text, new Font(this.Font.Name, 10), new SolidBrush(tabData.ForeColor), e.PageBounds.Left + (e.PageBounds.Width / 2), height, sf);
        }

不明白为什么我必须混合使用PageBounds和MarginBounds来使图像居中,然后使用文本将同时以MarginBounds或两个PageBounds居中

以下对我有用。 如果边距PageBounds则可能需要使用PageBounds

    void pd_PrintPage(object sender, PrintPageEventArgs e)
    {
        int w = e.MarginBounds.Width / 2;
        int x = e.MarginBounds.Left;
        int y = e.MarginBounds.Top;
        Font printFont = new Font("Arial", 10);
        Bitmap logo = System.Drawing.SystemIcons.WinLogo.ToBitmap();

        int height = 100 + y;
        string tabDataText = "Hello World";
        var tabDataForeColor = Color.Blue;
        var txtDataWidth = e.Graphics.MeasureString(tabDataText, printFont).Width;

        e.Graphics.DrawImage(logo,
            e.MarginBounds.Left + (e.MarginBounds.Width / 2) - (logo.Width / 2),
            e.MarginBounds.Top + (e.MarginBounds.Height / 2) - (logo.Height));

        using (var sf = new StringFormat())
        {
            height += logo.Height + 15;
            sf.LineAlignment = StringAlignment.Center;
            sf.Alignment = StringAlignment.Center;
            e.Graphics.DrawString(tabDataText, new Font(this.Font.Name, 10),
                 new SolidBrush(tabDataForeColor),
                 e.MarginBounds.Left + (e.MarginBounds.Width / 2),
                 e.MarginBounds.Top + (e.MarginBounds.Height / 2) + (logo.Height / 2) + 15,
                 sf);
        }

        e.HasMorePages = false;
    }

编辑回应

使用新代码输出。 您是说这就是您想要的吗?

还是您想要这个?

页边距是位于页面内部的矩形。 这些边距可能是不对称的,因此,如果要使用绝对中心,则应引用PageBounds

此外,您的文本是居中对齐的,因此使绘制文本的参考点位于String的中间,而不是logo左上角。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM