I'm trying to print multiple QR images in 1 page, but when i print i receive the last image only, the Code is:

int QRLoop = Convert.ToInt32(txtSerialTo.Text) - Convert.ToInt32(txtSerialFrom.Text) + 1;
for (int i = 0; i < QRLoop; i++)
{

int iSerial = Convert.ToInt32(txtSerialFrom.Text) + i;
string QRString = iSerial.ToString() + "," + dtpDate.Value.ToShortDateString() + "," + JobNr + "," + Batch + "," + LineNr + "," + Model;
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(QRString, QRCodeGenerator.ECCLevel.L, false, false, QRCodeGenerator.EciMode.Default, -1);
QRCode qrCode = new QRCode(qrCodeData);
Bitmap qrCodeImage = qrCode.GetGraphic(1);
pbx[i].Image = qrCode.GetGraphic(1);
}

xx = 30;
yy = 0;
int x = 0;
for(int h = 0; h < QRLoop - 1; h++)
{
    p.PrintPage += delegate (object sender1, PrintPageEventArgs e1)
    {
        e1.Graphics.DrawImage(pbx[h].Image, xx, yy);
        e1.Graphics.DrawString((h + 1).ToString(), new System.Drawing.Font("Arial", 9, FontStyle.Bold), new SolidBrush(Color.Black), new RectangleF(xx + 45, yy + 5, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height));
    };
    xx += 91;
    x++;
    if (x == 4)
    {
        x = 0;
        xx = 30;
        yy += 47;
    }
}
p.Print();

Any help?

Recommended Answers

What type is pbx?
Did you not try to debug and set some breakpoints?

Jump to Post

When you debugged, what was the value of QRLoop?

Jump to Post

All 5 Replies

What type is pbx?
Did you not try to debug and set some breakpoints?

pbx is a picturebox to save the QR code in it

and yes i tried to debug with breakpoints and it was looping perfectly, but it prints only the last image, even i tried to do what loop does manually to check the problem, such as:

p.PrintPage += delegate (object sender1, PrintPageEventArgs e1)
{
e1.Graphics.DrawImage(pbx[0].Image, 30, 0);
e1.Graphics.DrawString((1).ToString(), new System.Drawing.Font("Arial", 9, FontStyle.Bold), new SolidBrush(Color.Black), new RectangleF(75, 5, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height));
};

p.PrintPage += delegate (object sender1, PrintPageEventArgs e1)
{
e1.Graphics.DrawImage(pbx[1].Image, 121, 0);
e1.Graphics.DrawString((1).ToString(), new System.Drawing.Font("Arial", 9, FontStyle.Bold), new SolidBrush(Color.Black), new RectangleF(166, 5, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height));
};

p.PrintPage += delegate (object sender1, PrintPageEventArgs e1)
{
e1.Graphics.DrawImage(pbx[2].Image, 212, 0);
e1.Graphics.DrawString((1).ToString(), new System.Drawing.Font("Arial", 9, FontStyle.Bold), new SolidBrush(Color.Black), new RectangleF(257, 5, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height));
};

p.PrintPage += delegate (object sender1, PrintPageEventArgs e1)
{
e1.Graphics.DrawImage(pbx[3].Image, 303, 0);
e1.Graphics.DrawString((1).ToString(), new System.Drawing.Font("Arial", 9, FontStyle.Bold), new SolidBrush(Color.Black), new RectangleF(348, 5, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height));
};

p.Print();

and it printed the 4 images perfectly!!!

When you debugged, what was the value of QRLoop?

QRLoop Valued 16, but it could be changed, it's dynamic

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.