Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone
I have some paper forms that have some unfilled parts and i want to fill them programmatically via my dot matrix printer on my ready paper forms. so i made a panel which has some labels. and these labels have specified locations on the panel. but i can't print them without panel's background.
this is a part of my code that gives labels their text and print them:
private void button5_Click(object sender, EventArgs e)
        {
            int k=0;
            for (int i = 0; i < dataGridView1.RowCount && k < 3878; i++)
            {
                k = Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value);
                ctot25 = Convert.ToInt32(dataGridView1.Rows[i].Cells[13].Value);
                ctot50 = Convert.ToInt32(dataGridView1.Rows[i].Cells[14].Value);
                ctot100 = Convert.ToInt32(dataGridView1.Rows[i].Cells[15].Value);

                if (i > 0 && ctot25 != 0 || ctot50 != 0 || ctot100 != 0)
                {
                    label33.Text = (dataGridView1.Rows[i].Cells[1].Value).ToString();
                    label34.Text = (dataGridView1.Rows[i].Cells[0].Value).ToString();
                    System.Drawing.Printing.PrintDocument doc = new                System.Drawing.Printing.PrintDocument();
            doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
            PrintDialog PrintSettings = new PrintDialog();
            PrintSettings.Document = doc;
            PageSettings pgsetting = new PageSettings();

            if (PrintSettings.ShowDialog() == DialogResult.OK)
                doc.Print();
                }
            }

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Bitmap bmp = new Bitmap(panel1.Width, panel1.Height, panel1.CreateGraphics());
            panel1.DrawToBitmap(bmp, new Rectangle(0, 0, panel1.Width, panel1.Height));

            RectangleF bounds = e.PageSettings.PrintableArea;
            e.Graphics.DrawImage(bmp, bounds.Left, bounds.Top, panel1.Width, panel1.Height);
        }



at the end of each for I should have one of the forms printed.
Posted
Updated 15-Aug-14 21:17pm
v3

1 solution

The problem is that you are just drawing the panel into a bitmap - so you will get exactly teh same thing as if you had drawn it onto the screen - the panel doesn't "know" where it is drawing to (and rightly, it shouldn't need to know).

If you want to lose the background, you will have to draw the panel content yourself - or get the content to draw it itself, depending on what the panel contains - by looking at the panel Controls and working out what you want to do with them.

Sorry if that sounds vague, but from the info you give, we can't be precise.

BTW: don't use CreateGraphics like that: you are responsible for cleaning up after yourself, and you can't do that if you just pass it to a Bitmap constructor. Graphics contexts are a scarce resource, and you will run out of them if you keep coding like that! You need to keep a reference the the Graphics object and Dispose it when you are finished with it.
 
Share this answer
 
Comments
Member 10940906 16-Aug-14 3:41am    
thanks for your answer. but i'm new to the c#.i will really appreciate if you could show me some code to do the right thing. how can i draw just the content?
OriginalGriff 16-Aug-14 4:14am    
As I said: it depends on what the panel contains.
If it's buttons, then why are you printing them at all? :laugh:
If it's just text elements, then why not print them as text?
And so on - I can't tell because I can't see your screen!
Member 10940906 16-Aug-14 4:28am    
i don't know how to put the picture of my form in here,but i only have labels.i have some papers that somethig is writen on them and there are two empty fields.these fields are going to be filled by my labels.
OriginalGriff 16-Aug-14 4:48am    
Then just forget about them, and use Graphics.DrawString to print the label Text property directly on your PrintDocument. That way you can specify the exact place the text is going to be printed at, the size and font used and so forth.
Just get the name of the label, get the Text and print it!

http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawstring(v=vs.110).aspx
Member 10940906 16-Aug-14 5:06am    
thank you so so much :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900