C# 调整面板大小以打印整页
我在tabcontrolpanel上有一个面板,大约占屏幕的35%。我正在打印面板,它会打印屏幕上的确切尺寸。在C和winforms中,即使必须打印图像,也可以将图像缩放到8.5x11张纸上打印吗 这是我当前的代码-C# 调整面板大小以打印整页,c#,winforms,printing,panel,C#,Winforms,Printing,Panel,我在tabcontrolpanel上有一个面板,大约占屏幕的35%。我正在打印面板,它会打印屏幕上的确切尺寸。在C和winforms中,即使必须打印图像,也可以将图像缩放到8.5x11张纸上打印吗 这是我当前的代码- private void btnPrint_Click(object sender, EventArgs e) { PrintDocument doc = new PrintDocument(); doc.PrintPage += new SPrintPageEve
private void btnPrint_Click(object sender, EventArgs e)
{
PrintDocument doc = new PrintDocument();
doc.PrintPage += new SPrintPageEventHandler(doc_PrintPage);
doc.Print();
}
private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap image = new Bitmap(panelToPrint.Width, panelToPrint.Height, panelToPrint.CreateGraphics());
panelToPrint.DrawToBitmap(image, new Rectangle(0, 0, panelToPrint.Width, panelToPrint.Height));
RectangleF bounds = e.PageSettings.PrintableArea;
float factor = ((float)image.Height / (float)image.Width);
e.Graphics.DrawImage(image, bounds.Left, bounds.Top, bounds.Width, factor * bounds.Width);
}
编辑
我试图修改我的语法,使其在默认情况下以横向模式打印,但我仍然得到与以前相同的输出,只是图像打印大致在页面顶部的15%
private void btnPrint_Click(object sender, EventArgs e)
{
System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
doc.DefaultPageSettings.Landscape = true;
PrintDialog pdi = new PrintDialog();
pdi.Document = doc;
if (pdi.ShowDialog() == DialogResult.OK)
{
doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(doc_PrintPage);
doc.Print();
}
else
{
MessageBox.Show("User cancelled the print job");
}
}
private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap image = new Bitmap(panelToPrint.Width, panelToPrint.Height, panelToPrint.CreateGraphics());
panelToPrint.DrawToBitmap(image, new Rectangle(0, 0, panelToPrint.Width, panelToPrint.Height));
RectangleF bounds = e.PageSettings.PrintableArea;
float factor = ((float)image.Height / (float)image.Width);
e.Graphics.DrawImage(image, bounds.Left, bounds.Top, bounds.Width, factor * bounds.Width);
}
control.DrawToBitmap正是这样做的。它将您在屏幕上看到的控件打印为位图。如果要以不同的大小打印,则必须在打印前调整面板的大小
更好的方法是使用DrawString等图形方法将包含在面板中的数据直接绘制到页面