[英]Graphics.DrawString center in printdocument width
I'm attempting to center a string on a printdocument. 我正在尝试在打印文档上将字符串居中。 I've done the following with an image and it works but doesn't seem to work the same with a string.
我已经对图像进行了以下操作,它可以工作,但对于字符串似乎却不起作用。
Here is the code that I used to center the image 这是我用来使图像居中的代码
e.Graphics.DrawImage(logo, (e.MarginBounds.Width / 2) - (logo.Width / 2), height);
The text i'm trying to center is being supply from a Tab in a TabControl 我试图居中的文本是从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);
}
I have also tried below and used string_size.Width /2 in place of txtData.Width 我也在下面尝试过,并使用string_size.Width / 2代替txtData.Width
SizeF string_size = e.Graphics.MeasureString(tabData.Text, tabData.Font);
EDIT 编辑
Current full code 当前完整代码
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);
}
Don't understand why I have to use a mixture of PageBounds and MarginBounds to center the Image then with the text it will center with either both MarginBounds or both PageBounds 不明白为什么我必须混合使用PageBounds和MarginBounds来使图像居中,然后使用文本将同时以MarginBounds或两个PageBounds居中
The following works for me. 以下对我有用。 You may need to use
PageBounds
if your margins are not uniform. 如果边距
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;
}
Edit Response 编辑回应
Output using your new code. 使用新代码输出。 Are you saying this is what you want?
您是说这就是您想要的吗?
Or are you wanting this? 还是您想要这个?
The margin is a rectangle that sits inside the page. 页边距是位于页面内部的矩形。 It is possible that those margins are asymmetrical so if you want the absolute center you should reference
PageBounds
. 这些边距可能是不对称的,因此,如果要使用绝对中心,则应引用
PageBounds
。
Additionally, your text is center aligned so that makes the reference point of drawing the text in the middle of the String instead of top left like the logo
. 此外,您的文本是居中对齐的,因此使绘制文本的参考点位于String的中间,而不是
logo
左上角。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.