调整面板大小以打印整页 [英] Resize Panel To Print Full Page
问题描述
我在 tabcontrolpanel 上有一个面板,约占屏幕的 35%.我正在打印面板,它打印出屏幕上的确切尺寸.是否可以在 C# 和 winforms 中缩放图像以在 8.5x11 纸张上打印(即使我必须横向打印)?
I have a panel on a tabcontrolpanel that takes up roughly 35% of the screen. I am printing the panel, and it prints the exact size it is on the screen. Is it possible in C# and winforms to scale the image to print on a 8.5x11 peice of paper (even if I have to print it landscape)?
这是我当前的代码 --
This is my current code --
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% 处
Edit
I tried to modify my syntax to by default print in landscape mode, but I am still getting the same output as before, just the image printing roughly on the top 15% of the page
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
正是如此.它将control
(如您在屏幕上看到的)打印到位图.如果你想以不同的尺寸打印它,你必须在打印前调整面板的大小.
control.DrawToBitmap
does exactly that. It prints the control
(as you see it on the screen) to a bitmap. If you want to print it in a different size you have to resize the panel before printing.
更好的方法是使用DrawString
等图形方法将面板中包含的数据直接绘制到页面上.
A better approach is to draw your data that is contained into the panel directly to the page using graphics methods like DrawString
etc.
这篇关于调整面板大小以打印整页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!