小弟新转C#一周 ,现在在完成一个打印的功能,
我用 一张图片做背景 当打印格式,然后在相应的地方添加数据,
我装了虚拟打印机,那个打印位置,我是一点一点的推测出来的,全都写死了,(有点欠妥当);
能不能有个方法来 获取打印机信息 获取不同打印机分辨率,那样根据打印机分辨率来设置来确定数据打印位置?private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//Create font and brush
Font drawFont = new Font("黑体", 20);
Font drawFont1 = new Font("黑体", 16);
SolidBrush drawBrush = new SolidBrush(Color.Black); Image memoryImage = Image.FromFile(Application.StartupPath + @"\back.jpg"); //背景格式文件路径
e.Graphics.DrawImage(memoryImage, 0, 0, 825, 1178); Image memoryImage1 = Image.FromFile(Application.StartupPath + @"\top.jpg"); //测试头像文件路径
e.Graphics.DrawImage(memoryImage1, 583, 200, 170, 240);
//基本信息
e.Graphics.DrawString(name, drawFont, drawBrush, 150, 218);//这些数据位置全是一点点试出来的,不科学啊
e.Graphics.DrawString(sex, drawFont, drawBrush, 460, 218);
e.Graphics.DrawString(unit, drawFont, drawBrush, 150, 276);
e.Graphics.DrawString(site, drawFont, drawBrush, 150, 334);
e.Graphics.DrawString(Tel, drawFont, drawBrush, 150, 392);
}
我用 一张图片做背景 当打印格式,然后在相应的地方添加数据,
我装了虚拟打印机,那个打印位置,我是一点一点的推测出来的,全都写死了,(有点欠妥当);
能不能有个方法来 获取打印机信息 获取不同打印机分辨率,那样根据打印机分辨率来设置来确定数据打印位置?private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//Create font and brush
Font drawFont = new Font("黑体", 20);
Font drawFont1 = new Font("黑体", 16);
SolidBrush drawBrush = new SolidBrush(Color.Black); Image memoryImage = Image.FromFile(Application.StartupPath + @"\back.jpg"); //背景格式文件路径
e.Graphics.DrawImage(memoryImage, 0, 0, 825, 1178); Image memoryImage1 = Image.FromFile(Application.StartupPath + @"\top.jpg"); //测试头像文件路径
e.Graphics.DrawImage(memoryImage1, 583, 200, 170, 240);
//基本信息
e.Graphics.DrawString(name, drawFont, drawBrush, 150, 218);//这些数据位置全是一点点试出来的,不科学啊
e.Graphics.DrawString(sex, drawFont, drawBrush, 460, 218);
e.Graphics.DrawString(unit, drawFont, drawBrush, 150, 276);
e.Graphics.DrawString(site, drawFont, drawBrush, 150, 334);
e.Graphics.DrawString(Tel, drawFont, drawBrush, 150, 392);
}
补充一点,已经确定是要打印在 A4 的纸上。
额,我先去查下 MSDN....
现在出现了一个问题,就是为什么我获取到的 X Y 的值都是相同的?600 * 600 ; 1200 * 1200 ,可A4纸很明显是长方形的啊; 是打印机本身分辨率就是这样的,还是我代码哪里出现问题 了? //测试代码
string Look1 = e.Graphics.DpiX.ToString();
string Look2 = e.Graphics.DpiY.ToString();
e.Graphics.DrawString("X分辨率 " + Look1, drawFont1, drawBrush, 105, 640);
e.Graphics.DrawString("Y分辨率 " + Look2, drawFont1, drawBrush, 105, 690);
System.Drawing.Printing.PrinterSettings prtSet = new System.Drawing.Printing.PrinterSettings();
prtSet.PrinterName = cmdLocalPrinter.Text;
System.Drawing.Printing.PrinterResolution[] prlAry = new System.Drawing.Printing.PrinterResolution[prtSet.PrinterResolutions.Count];
prtSet.PrinterResolutions.CopyTo(prlAry, 0);
int dpi = prlAry[prtSet.PrinterResolutions.Count - 1].X;
比如:写死了参数的打印机的分辨率固定为dpiX dpiY
新的打印机分辨率为dpiNewX dpiNewY
定义比率为:dpiNewX/dpiX dpiNewY/dpiY
则:
e.Graphics.DrawString(name, drawFont, drawBrush, 150*dpiNewX/dpiX
, 218*dpiNewY/dpiY
);
可行?
有一个image, 其w和h也是知道的
然后 stretchblt就实现了
现在 “打印机 dc的width和height”这里解决不了