System.Drawing以精确的毫米为单位打印实线

时间:2012-09-04 11:31:57

标签: c# printing system.drawing

在这里使用System.Drawing时遇到麻烦。

我正在尝试通过请求打印机的dpi并通过GetDeviceCaps计算相应的像素来构建适用于此处所有打印机的例程。

我的测试打印机得到600 dpi,所以我使用TenthOfAmmToPixels计算像素,我使用十分之一毫米指定测量值。

2个问题:

  • 打印时测量关闭:30毫米。线尺34毫米。打印时
  • 印刷线根本不是实线,它在屏幕上,但是当从打印机中滚出时,它在线条周围有一个奇怪的虚线阴影。

    Image bmIm;
    
    private void PrintImage(Image img)
    {
        bmIm = img;
    
        PrintDocument pd = new PrintDocument();
    
        pd.OriginAtMargins = true;
        pd.DefaultPageSettings.Landscape = true;
    
        pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
    
        pd.Print();
    }
    
    
    public class PrinterBounds
    {
      [DllImport("gdi32.dll")]
    private static extern Int32 GetDeviceCaps(IntPtr hdc, Int32 capindex);
    
    private const int HORZRESoffset = 8;
    private const int HORZSIZEoffset = 4;
    
    
    public readonly int HORZRES;
    public readonly int HORZSIZE;
    
    public PrinterBounds(PrintPageEventArgs e)
    {
        IntPtr hDC = e.Graphics.GetHdc();
    
        HORZRES = GetDeviceCaps(hDC, HORZRESoffset);
        HORZSIZE = GetDeviceCaps(hDC, HORZSIZEoffset);
    
        e.Graphics.ReleaseHdc(hDC);
    }
    

    }

       void pd_PrintPage(object sender, PrintPageEventArgs e)
     {
        PrinterBounds objBounds = new PrinterBounds(e);
    
        var HORZSIZE = objBounds.HORZSIZE;
        var HORZRES = objBounds.HORZRES;
    
        double dpi = 254 * HORZRES / HORZSIZE / 10;
    
        Bitmap label = new Labels().GetPackingLabel("000100000001", dpi);
    
        e.Graphics.DrawImage(label, 0, 0);
    

    }

     public class Labels
     {
    public Bitmap GetPackingLabel(Bandit bandit, double dblDpiDevice)
    {
        int dpiDevice = Convert.ToInt32(dblDpiDevice);
    
        Bitmap label = new Bitmap(TenthOfAmmToPixels(1000, dpiDevice), TenthOfAmmToPixels(1000, dpiDevice));
    
        Graphics g = Graphics.FromImage(label);
    
        String drawString = "Sample Text";
        Font drawFont = new Font("Arial", 16);
        SolidBrush drawBrush = new SolidBrush(Color.Black);
    
        Pen pen = new Pen(drawBrush);
        pen.DashStyle = DashStyle.Solid;
    
        StringFormat drawFormat = new StringFormat();
        drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
    
        g.DrawString("test string", drawFont, drawBrush, TenthOfAmmToPixels(100, dpiDevice), TenthOfAmmToPixels(100, dpiDevice));
    
        //3 cm?
        g.DrawLine(pen, TenthOfAmmToPixels(0, dpiDevice), TenthOfAmmToPixels(350, dpiDevice), TenthOfAmmToPixels(300, dpiDevice), TenthOfAmmToPixels(350, dpiDevice));
    
    
        return label;
    
    }
    
    public int TenthOfAmmToPixels(double tiendes, double ppi)
    {
        double result = (double)(tiendes * ppi / 254);
    
        return Convert.ToInt32(result);
    
    }
    

    }

0 个答案:

没有答案