如何将两个 bmp 打印到两个不同的页面?
How to print two bmp into two different pages?
我正在尝试的方法是生成两个重叠的图像。
我可以制作两个不同的按钮来打印两个单独的 bmp,但我需要将两个 bmp 打印在同一页上,因为用户可以选择 PDF 打印机并生成一个两页文件。
void Imprimir()
{
PrintDocument pd = new PrintDocument();
pd.DocumentName = "Relatório SisIndice";
pd.PrintPage += new PrintPageEventHandler(doc_PrintPage);
pd.DefaultPageSettings.Landscape = true;
PrintDialog printDialog1 = new PrintDialog();
printDialog1.Document = pd;
DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
{
pd.Print();
}
}
private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap bmp = new Bitmap(pnlPrint.Width, pnlPrint.Height, pnlPrint.CreateGraphics());
pnlPrint.DrawToBitmap(bmp, new Rectangle(0, 0, pnlPrint.Width, pnlPrint.Height));
RectangleF bounds = e.PageSettings.PrintableArea;
float factor = ((float)bmp.Height / (float)bmp.Width);
e.Graphics.DrawImage(bmp, bounds.Left, bounds.Top, 1118, 855);
e.HasMorePages = true;
Bitmap bmp1 = new Bitmap(dgvDetGraf.Width, dgvDetGraf.Height, dgvDetGraf.CreateGraphics());
dgvDetGraf.DrawToBitmap(bmp1, new Rectangle(0, 1800, dgvDetGraf.Width, dgvDetGraf.Height));
RectangleF bounds1 = e.PageSettings.PrintableArea;
e.Graphics.DrawImage(bmp1, bounds1.Left, bounds1.Top, 894, 684);
e.HasMorePages = false;
}
您很接近,但您错过了 PrintPage
事件针对单个页面触发的情况。在这种情况下,您必须对其 Graphics
对象执行所有 painting/drawing。当没有更多页面时,您将 HasMorePages
设置为 false。
因此,您不必一次绘制所有这些位图,而是必须跟踪您是在第 1 页还是第 2 页上,并基于此在该页上绘制所需的位图。
我选择了可能可行的最简单的解决方案,它涉及一个 page
变量来跟踪我们所在的页面。
int page = 0;
void Imprimir()
{
// snip irrelevant stuf
if (result == DialogResult.OK)
{
// reset our state to page 1
page = 1;
pd.Print();
}
}
private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
// this gets called twice, the page variable
// keeps track of what to do (it keeps the State)
switch(page)
{
// do this for page 1
case 1:
Bitmap bmp = new Bitmap(pnlPrint.Width, pnlPrint.Height, pnlPrint.CreateGraphics());
pnlPrint.DrawToBitmap(bmp, new Rectangle(0, 0, pnlPrint.Width, pnlPrint.Height));
RectangleF bounds = e.PageSettings.PrintableArea;
float factor = ((float)bmp.Height / (float)bmp.Width);
e.Graphics.DrawImage(bmp, bounds.Left, bounds.Top, 1118, 855);
e.HasMorePages = true;
break;
// do this for page 2
case 2:
Bitmap bmp1 = new Bitmap(dgvDetGraf.Width, dgvDetGraf.Height, dgvDetGraf.CreateGraphics());
dgvDetGraf.DrawToBitmap(bmp1, new Rectangle(0, 1800, dgvDetGraf.Width, dgvDetGraf.Height));
RectangleF bounds1 = e.PageSettings.PrintableArea;
e.Graphics.DrawImage(bmp1, bounds1.Left, bounds1.Top, 894, 684);
e.HasMorePages = false;
break;
}
// don't forget to go the next state ...
page++; // increase page counter
}
我正在尝试的方法是生成两个重叠的图像。
我可以制作两个不同的按钮来打印两个单独的 bmp,但我需要将两个 bmp 打印在同一页上,因为用户可以选择 PDF 打印机并生成一个两页文件。
void Imprimir()
{
PrintDocument pd = new PrintDocument();
pd.DocumentName = "Relatório SisIndice";
pd.PrintPage += new PrintPageEventHandler(doc_PrintPage);
pd.DefaultPageSettings.Landscape = true;
PrintDialog printDialog1 = new PrintDialog();
printDialog1.Document = pd;
DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
{
pd.Print();
}
}
private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap bmp = new Bitmap(pnlPrint.Width, pnlPrint.Height, pnlPrint.CreateGraphics());
pnlPrint.DrawToBitmap(bmp, new Rectangle(0, 0, pnlPrint.Width, pnlPrint.Height));
RectangleF bounds = e.PageSettings.PrintableArea;
float factor = ((float)bmp.Height / (float)bmp.Width);
e.Graphics.DrawImage(bmp, bounds.Left, bounds.Top, 1118, 855);
e.HasMorePages = true;
Bitmap bmp1 = new Bitmap(dgvDetGraf.Width, dgvDetGraf.Height, dgvDetGraf.CreateGraphics());
dgvDetGraf.DrawToBitmap(bmp1, new Rectangle(0, 1800, dgvDetGraf.Width, dgvDetGraf.Height));
RectangleF bounds1 = e.PageSettings.PrintableArea;
e.Graphics.DrawImage(bmp1, bounds1.Left, bounds1.Top, 894, 684);
e.HasMorePages = false;
}
您很接近,但您错过了 PrintPage
事件针对单个页面触发的情况。在这种情况下,您必须对其 Graphics
对象执行所有 painting/drawing。当没有更多页面时,您将 HasMorePages
设置为 false。
因此,您不必一次绘制所有这些位图,而是必须跟踪您是在第 1 页还是第 2 页上,并基于此在该页上绘制所需的位图。
我选择了可能可行的最简单的解决方案,它涉及一个 page
变量来跟踪我们所在的页面。
int page = 0;
void Imprimir()
{
// snip irrelevant stuf
if (result == DialogResult.OK)
{
// reset our state to page 1
page = 1;
pd.Print();
}
}
private void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
// this gets called twice, the page variable
// keeps track of what to do (it keeps the State)
switch(page)
{
// do this for page 1
case 1:
Bitmap bmp = new Bitmap(pnlPrint.Width, pnlPrint.Height, pnlPrint.CreateGraphics());
pnlPrint.DrawToBitmap(bmp, new Rectangle(0, 0, pnlPrint.Width, pnlPrint.Height));
RectangleF bounds = e.PageSettings.PrintableArea;
float factor = ((float)bmp.Height / (float)bmp.Width);
e.Graphics.DrawImage(bmp, bounds.Left, bounds.Top, 1118, 855);
e.HasMorePages = true;
break;
// do this for page 2
case 2:
Bitmap bmp1 = new Bitmap(dgvDetGraf.Width, dgvDetGraf.Height, dgvDetGraf.CreateGraphics());
dgvDetGraf.DrawToBitmap(bmp1, new Rectangle(0, 1800, dgvDetGraf.Width, dgvDetGraf.Height));
RectangleF bounds1 = e.PageSettings.PrintableArea;
e.Graphics.DrawImage(bmp1, bounds1.Left, bounds1.Top, 894, 684);
e.HasMorePages = false;
break;
}
// don't forget to go the next state ...
page++; // increase page counter
}