【发布时间】:2016-06-03 16:03:18
【问题描述】:
我正在使用 WinForms。在我的表单中,我有一个图片框和(一个发件人:文本框和一个收件人:文本框)。这些文本框用于打印多页 Tif 文档中的某些页面范围。问题是应用程序不打印页面范围。另一个问题是打印预览没有显示正确的页面,例如,如果我在 From 文本框中键入数字 2,我希望打印预览对话框显示 Tif 文档中的第 2 页,它不会显示它显示错误的页面是第 1 页。
测试 1:假设我想从我输入的 tif 文档中打印第 2-5 页(从:2,到:5)。 奇怪的是应用程序只会打印第 2 页。
-
测试 2:我在
print_preview_Setting()下添加了下面的行,由于某种原因,打印范围使用它,但奇怪的是打印预览仍然显示错误的页面。if (printDialog1.ShowDialog() == DialogResult.OK) { currentPrintPage = Convert.ToInt32(From_Pg_txtBox.Text) - 1; printDocument1.Print(); }
注意:我一直在为我的测试用例打印成 PDF
以下是用于测试的示例 Tif 文档
http://www.filedropper.com/tifbordernumberpage
using System.Drawing.Printing;
using System.Drawing.Imaging;
private int currentPrintPage;
private void Form1_Load(object sender, EventArgs e)
{
//using (var dialog = new OpenFileDialog())
//{
// if (dialog.ShowDialog(this) == DialogResult.OK)
// {
// string filename = dialog.FileName;
// pictureBox1.Load(filename);
// }
//}
pictureBox1.Load("C:\\image\\Tif_Document.tif");
}
private void Print_button_Click(object sender, EventArgs e)
{
print_preview_Settings();
// if (printDialog1.ShowDialog() == DialogResult.OK)
// {
// currentPrintPage = Convert.ToInt32(From_Pg_txtBox.Text) - 1;
// printDocument1.Print();
// }
}
private void print_preview_Settings()
{
printPreviewDialog1.Document = printDocument1;
printDocument1.DefaultPageSettings.Margins.Top = 100;
printDocument1.DefaultPageSettings.Margins.Left = 200;
printDocument1.DefaultPageSettings.Margins.Right = 0;
printDocument1.DefaultPageSettings.Margins.Bottom = 0;
currentPrintPage = Convert.ToInt32(From_Pg_txtBox.Text) - 1;
printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
Image i;
i = Image.FromFile("C:\\image\\Tif_Document.tif");
i.SelectActiveFrame(FrameDimension.Page, currentPrintPage);
//Print while maintating aspect ratio of the image
var img_width = e.PageBounds.Width - e.MarginBounds.Left - Math.Abs(e.MarginBounds.Right - e.PageBounds.Width);
var img_height = e.PageBounds.Height - e.MarginBounds.Top - Math.Abs(e.MarginBounds.Bottom - e.PageBounds.Height);
var img = ResizeAcordingToImage(i, img_width, img_height);
e.Graphics.DrawImage(i,
e.MarginBounds.Left, e.MarginBounds.Top, img.Width, img.Height);
currentPrintPage++; //increment page from the Tif doc
if (currentPrintPage < Convert.ToInt32(to_Pg_txtBox.Text))
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
}
private Image ResizeAcordingToImage(Image Source, int boxWidth, int boxHeight)
{
Image resizedImage;
double dbl = (double)Source.Width / (double)Source.Height;
//set height of image to boxHeight and check if resulting width is less than boxWidth,
//else set width of image to boxWidth and calculate new height
if ((int)((double)boxHeight * dbl) <= boxWidth)
{
resizedImage = new Bitmap(Source, (int)((double)boxHeight * dbl), boxHeight);
}
else
{
resizedImage = new Bitmap(Source, boxWidth, (int)((double)boxWidth / dbl));
}
return resizedImage;
}
【问题讨论】:
-
你不是检查错了边界吗:
if (currentPrintPage < Convert.ToInt32(From_Pg_txtBox.Text)) -
对不起,我忘记修改了。 @LarsTech
-
我认为您编辑了错误的行。
-
哎呀,感谢您注意到这一点。
标签: c# .net image winforms printing