日期:2014-05-17  浏览次数:22004 次

C# 如何获取打印机分辨率
小弟新转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);


------解决方案--------------------
e.Graphics.DpiX 水平分辨率

e.Graphics.DpiY 垂直分辨率


------解决方案--------------------
            //获取打印机分辨率
            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;
------解决方案--------------------
你确定分辨率怎么写公式安排坐标啊?