C# 在C中的for循环中打印#
我试图在for循环中打印,其中必须打印一个groupbox,直到满足条件为止 //代码:C# 在C中的for循环中打印#,c#,.net,winforms,printing,C#,.net,Winforms,Printing,我试图在for循环中打印,其中必须打印一个groupbox,直到满足条件为止 //代码: private void btnPrint_Click(object sender, EventArgs e) { for (int i = 1; i <= Convert.ToInt32(lblTotalBox.Text); i++) { lblBoxNumber.Text = i.ToString();
private void btnPrint_Click(object sender, EventArgs e)
{
for (int i = 1; i <= Convert.ToInt32(lblTotalBox.Text); i++)
{
lblBoxNumber.Text = i.ToString();
printDocument1.Print();
}
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
PaperSize paperSize = new PaperSize("MyCustomSize", 100, 65);
paperSize.RawKind = (int)PaperKind.Custom;
printDocument1.DefaultPageSettings.PaperSize = paperSize;
using (Graphics g = e.Graphics)
{
using (new Font("Arial", 16))
{
float x = new float();
float y = new float();
x = e.MarginBounds.Left;
y = e.MarginBounds.Top;
Bitmap bmp = new Bitmap(350, 400);
grpReceipt.DrawToBitmap(bmp, new Rectangle(0, 0, 350, 400));
e.Graphics.DrawImage(bmp, x, y);
}
}
}
private void btnPrint\u单击(对象发送者,事件参数e)
{
对于(int i=1;i我怀疑所有其他打印不是空的,它们只是相互重叠。您应该这样做:
int nextTop = -1;
int i = 1;
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) {
for(;i <= Convert.ToInt32(lblTotalBox.Text); i++){
lblBoxNumber.Text = i.ToString();
using (Graphics g = e.Graphics) {
int y = nextTop == -1 ? e.MarginBounds.Top : nextTop;
Bitmap bmp = new Bitmap(350, 400);
grpReceipt.DrawToBitmap(bmp, new Rectangle(0, 0, 350, 400));
g.DrawImage(bmp, e.MarginBounds.Left, y);
nextTop += bmp.Height + 10;
if(nextTop > e.MarginBounds.Height - bmp.Height) {
nextTop = -1
e.HasMorePages = true;
return;
}
}
}
}
private void btnPrint_Click(object sender, EventArgs e) {
i = 1;
PaperSize paperSize = new PaperSize("MyCustomSize", 100, 65);
paperSize.RawKind = (int)PaperKind.Custom;
printDocument1.DefaultPageSettings.PaperSize = paperSize;
printDocument1.Print();
}
int nextTop=-1;
int i=1;
私有void printDocument1_PrintPage(对象发送方,PrintPageEventArgs e){
对于(;即MarginBounds.Height-bmp.Height){
nextTop=-1
e、 HasMorePages=true;
返回;
}
}
}
}
私有void btnPrint\u单击(对象发送方,事件参数e){
i=1;
PaperSize PaperSize=新纸张尺寸(“MyCustomSize”,100,65);
paperSize.RawKind=(int)PaperKind.Custom;
printDocument1.DefaultPageSettings.PaperSize=纸张大小;
printDocument1.Print();
}
这里是错误的地方。在循环中调用printDocument1.Print()
试试这个:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
PrintDocument pd=new PrintDocument();
int index=0, count=0;
pd.BeginPrint+=(s, ev) =>
{
// find page count from form label
count=int.Parse(label1.Text);
};
pd.PrintPage+=(s, ev) =>
{
// for each page
index++;
// get form size
var size=this.Size;
// create bitmap of same size
var bmp=new Bitmap(size.Width, size.Height);
// draw form into bitmap
this.DrawToBitmap(bmp, new Rectangle(Point.Empty, size));
// draw bitmap into graphics, resize to fit paper margins
ev.Graphics.DrawImage(bmp, new Rectangle(ev.MarginBounds.Location, ev.MarginBounds.Size));
// create a font and draw on graphics the page number
using(var font = new Font(FontFamily.GenericSansSerif, 16f))
{
ev.Graphics.DrawString(index.ToString(), font, Brushes.Black, ev.MarginBounds.Location);
}
// check for final page
ev.HasMorePages=index<count;
};
pd.EndPrint+=(s, ev) =>
{
// reset count and index
index=0;
count=0;
};
PaperSize paper=new PaperSize("MyCustomSize", 100, 65);
paper.RawKind=(int)PaperKind.Custom;
// set paper size
pd.DefaultPageSettings.PaperSize=paper;
// set paper margins appropriately
pd.DefaultPageSettings.Margins=new Margins(10, 10, 10, 10);
// call up the print preview dialog to see results
PrintPreviewDialog dlg=new PrintPreviewDialog();
dlg.Document=pd;
dlg.ShowDialog();
}
}
它只抓取用于绘图的groupbox
迭代之前,将.Convert.ToInt32(lblTotalBox.Text
返回什么?这将是常量。可以是5-10之间的任意数字。什么是printDocument1.Print()?您能将语句包装在一个try-catch中,并在一个可能的异常上设置断点吗?也许可以在.Print处跟踪调用?我的想法是.Print正在抛出一个未发生的异常,而不是使它进入main(),这样应用程序就不会崩溃…(请注意,这是一个疯狂的猜测!)您不需要使用(Graphics g=e.Graphics){}
因为您没有创建新的图形对象,而是为现有的图形对象指定了一个引用。您也没有在代码中使用g
。我正在打印整个表单。我只需要groupbox。请在帖子中包含表单的图片,以便我们知道您在说什么。无论如何,请更改var size=this.size;
更改位图的大小,并调整this.DrawToBitmap()
命令以适应所需的UI元素。添加了表单图像。请更新代码以仅打印groupbox。整个内容是groupbox,还是您对其中一个框感兴趣。从发布内容来看,仍然不清楚什么是什么。所有控件都在groupbox中,我正在尝试打印整个groupbox。
// get form size
var size=groupBox1.Size;
// create bitmap of same size
var bmp=new Bitmap(size.Width, size.Height);
// draw form into bitmap
groupBox1.DrawToBitmap(bmp, new Rectangle(Point.Empty, size));