2

我可以打印打印预览对话框中的内容吗?我有这段代码,它没有打印打印预览对话框中的内容。我的意思是当我使用一张完整的证券纸 (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);
}
4

0 回答 0