我用printDocument组件打印票据,调用DrawString方法时,e.Graphics.DrawString(drawString, drawFont, drawBrush,x, y, drawFormat)
此处的x,y值能否以毫米为计量单位?如果可以,该如何设置?如果不可以,怎样才能实现以毫米为单位的坐标并打印?谢谢
此处的x,y值能否以毫米为计量单位?如果可以,该如何设置?如果不可以,怎样才能实现以毫米为单位的坐标并打印?谢谢
调试欢乐多
将你的要打印的事物 加入其中
再在图片上放Label
private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
double topMargin = e.MarginBounds.Top; // 获取图片的分辨率(像素/英寸)
double HResolution = this.pictureBox.Image.HorizontalResolution; // 水平分辨率
double VResolution = this.pictureBox.Image.VerticalResolution; // 垂直分辨率 // 设置打印单位为毫米
e.Graphics.PageUnit = GraphicsUnit.Millimeter;
// 底版图片大小(1/100英寸为单位)
int imageWidth = (int)(this.pictureBox.Image.Width * 100 / HResolution);
int imageHeight = (int)(this.pictureBox.Image.Height * 100 / VResolution); // 要打印的内容单位也转为毫米
foreach (Control ctrl in this.Controls)
{
if (ctrl.GetType() == typeof(Label))
{
float left = (float)((ctrl.Left -this.pictureBox.Left) * 25.4 / HResolution + this.adjustX);
float top = (float)((ctrl.Top -this.pictureBox.Top) * 25.4 / VResolution + this.adjustY + topMargin * 25.4 / 100);
float width = (float)(ctrl.Width * 25.4 / this.dpiX);
float height = (float)(ctrl.Height * 25.4 / this.dpiY); e.Graphics.DrawString(ctrl.Text, ctrl.Font, this.drawBrush, new RectangleF(left, top, width, height));
}
}
}