是否可以准确打印打印预览中的内容?

时间:2013-06-05 06:46:02

标签: c# graphics printing drawing print-preview

我可以打印打印预览对话框中的内容吗?我有这个代码,它没有打印打印预览对话框中的内容。我的意思是当我使用一个完整的证券纸(8.5x11)时,这个代码打印在债券纸的最左上角,但这不是我想要发生的。我需要将纸张切割成支票尺寸,然后收缩打印机的纸盘以放入纸张的确切尺寸(支票尺寸)。这可能吗?

这是我的代码:

Bitmap MemoryImage;
PrintDocument printdoc1 = new PrintDocument();
PrintPreviewDialog previewdlg = new PrintPreviewDialog();
Panel pannel = null;

public chequeLayout()
{
   InitializeComponent();
   //declare event handler for printing in constructor
   printdoc1.PrintPage += new PrintPageEventHandler(printdoc1_PrintPage);
}

public void GetPrintArea(Panel pnl)
{            
   MemoryImage = new Bitmap(pnl.Width, pnl.Height);
   Rectangle rect = new Rectangle(0, 0, pnl.Width, pnl.Height);
   pnl.DrawToBitmap(MemoryImage, new Rectangle(0,0, pnl.Width, pnl.Height));
}

protected override void OnPaint(PaintEventArgs e)
{
  if (MemoryImage != null)
  {
      e.Graphics.DrawImage(MemoryImage, 0,0);
      base.OnPaint(e);                
  }
}

public void printdoc1_PrintPage(object sender, PrintPageEventArgs e)
{
   Rectangle pagearea = e.PageBounds;
   e.Graphics.RotateTransform(-90);
   yPrintCoordinate = ((CentimeterToPixel(Convert.ToDouble(txtWidth.Text))) - (2 * (CentimeterToPixel(Convert.ToDouble(txtWidth.Text))))) - 32;
   e.Graphics.DrawImage(MemoryImage, yPrintCoordinate, 0);
}

public void Print(Panel pnl)
{        
   pannel = pnl;
   GetPrintArea(pnl);
   printdoc1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("Cheque Size", CentimeterToPixel(Convert.ToDouble(heightSize)), (CentimeterToPixel(Convert.ToDouble(widthSize))));
   previewdlg.Document = printdoc1;
   previewdlg.ShowDialog();
}

int CentimeterToPixel(double Centimeter)
{
   double pixel = -1;
   using (Graphics g = this.CreateGraphics())
   {
    pixel = Centimeter * g.DpiY / 2.54d;
   }
   return (int)pixel;
}

private void btnPrint_Click(object sender, EventArgs e) 
{
   Print(panel1);
}

0 个答案:

没有答案