Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Guys,

please help me out, I had publish a C#.net project and after publishing my print is not same , before publishing it is as expected by client.

below is my print code.

I have added code also...

What I have tried:

<pre>  Bitmap bitmap;
        private void btnPrint_Click(object sender, EventArgs e)
        {
            panel1.AutoScroll = false;
            btnPrint.Visible = false;
            ddlPrinters.Visible = false;
            //Add a Panel control.
            Panel panel = new Panel();
            this.Controls.Add(panel);
            //   this.Controls.Add(panel1);

            //Create a Bitmap of size same as that of the Form.
            //Graphics grp = panel1.CreateGraphics();
             Graphics grp = panel.CreateGraphics();
            Size formSize = this.panel1.ClientSize;
            bitmap = new Bitmap(formSize.Width, formSize.Height, grp);
            grp = Graphics.FromImage(bitmap);

            //Copy screen area that that the Panel covers.
            Point panelLocation = PointToScreen(panel.Location);
            //Point panelLocation = PointToScreen(panel1.Location);
              grp.CopyFromScreen(panelLocation.X, panelLocation.Y, 0, 0, formSize);

            ////Show the Print Preview Dialog.
            //printPreviewDialog1.Document = printDocument1;
            //printPreviewDialog1.PrintPreviewControl.Zoom = 1;
            //printPreviewDialog1.ShowDialog();
           // printDialog1.ShowDialog();
            PrintCard();
        }
        private void PrintCard()
        {
            StandardPrintController spController = new StandardPrintController();

            this.printDocument1 = new PrintDocument();
            this.printDocument1.PrintPage += new PrintPageEventHandler(this.printDocument1_PrintPage);
            //this.m_printDocument.PrinterSettings.PrinterName = this.strPrinterName;
            this.printDocument1.PrinterSettings.PrinterName = this.ddlPrinters.Text;
            this.printDocument1.DefaultPageSettings.Landscape = true;
            // this.printDocument1 = 1;
            //  this.printDocument1 = 2;
            this.printDocument1.DefaultPageSettings.Margins = new Margins(0, 20, 0, 0);
            this.printDocument1.DefaultPageSettings.Landscape = true;
            this.printDocument1.PrinterSettings.Duplex = Duplex.Horizontal;
            this.printDocument1.PrinterSettings.DefaultPageSettings.Color = true;
            // this.printDocument1.QueryPageSettings += new QueryPageSettingsEventHandler(this.printDocument1_QueryPageSettings);
            this.printDocument1.PrinterSettings.Copies = 1;
            this.printDocument1.PrintController = spController;
            this.printDocument1.OriginAtMargins = true;
            //  this.printDocument1 = 0;
            this.printDocument1.DocumentName = "QR Printing";
            this.printDocument1.Print();

        }

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            //Print the contents.
            // e.Graphics.DrawImage(bitmap, 140, 20);
            e.Graphics.DrawImage(bitmap, 20, 60);

        }
Posted
Updated 27-Jul-21 19:45pm

1 solution

We can't help: you are printing an image of a screen, and what gets printed will depend on the settings of the screen on the actual hardware the code runs on: so a small change in default font size, colour depth, zoom factor applied across the system will impact the output.
And we have no access to either computer!

What I will say is this: that's a very poor idea. It's simple to implement, but it will always give problems like this. Instead, use the PrintDocument to use the information inside your panel to generate a print page using Graphics.DrawString and suchlike instead of assuming that "all screens are the same" and trying to just grab an image.
 
Share this answer
 
Comments
badar codepro 28-Jul-21 10:08am    
its not another hardware, i am publishing and running on same server and i used printdocument for printing you can check my code , can you elaborate how to use Graphics.DrawString?
OriginalGriff 28-Jul-21 10:32am    
See the example in the documentation:
https://docs.microsoft.com/en-us/dotnet/api/system.drawing.printing.printdocument?view=net-5.0

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