Mehr Muhammad Hamza
Mehr Muhammad Hamza

Reputation: 115

How to Print full width window form C#.?

I am trying to Print the complete form but It print document is not covering full width. I have tried different ways, But didn't get the desired result. I have taken help from this link https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-print-a-windows-form

This is the half print which I am getting This is the result I am getting

Here is the complete form which I want to print This is the Complete form which I want to print completely

Please tell me how I can get complete form printed. Thanks in advance

Upvotes: 1

Views: 1843

Answers (2)

Yusuf KALAY
Yusuf KALAY

Reputation: 1

If you want to fix size When Form starts you can try this, my friend. Or When you click the print button, some controls are added to your form you can only just call the SizeFix function end of your print button click event function instead of creating load event.

        public Form1()
        {
            InitializeComponent();
            this.Load += new EventHandler(SizeFix);
        }


        public void SizeFix(object o, EventArgs e)
        {
            int widthMax = this.Width;
            foreach (var item in this.Controls)
            {
                int tempWitdh = (item as Control).Location.X + (item as Control).Width;
                if (tempWitdh > widthMax)
                {
                    widthMax = tempWitdh;
                }
            }

            this.Width = widthMax;  // You can add 10 width more
        }

Upvotes: 0

Bhavesh Patel
Bhavesh Patel

Reputation: 136

I am not sure about the link you are following, I was getting same error even after trying multiple codes while I was trying to print whole form once. Then I found below code somewhere on internet, which prints whole form.

    private System.IO.Stream streamToPrint;
    string streamType;
    PrintDocument printDoc = new PrintDocument();
    [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
    private static extern bool BitBlt
    (
        IntPtr hdcDest, // handle to destination DC  
        int nXDest, // x-coord of destination upper-left corner  
        int nYDest, // y-coord of destination upper-left corner  
        int nWidth, // width of destination rectangle  
        int nHeight, // height of destination rectangle  
        IntPtr hdcSrc, // handle to source DC  
        int nXSrc, // x-coordinate of source upper-left corner  
        int nYSrc, // y-coordinate of source upper-left corner  
        System.Int32 dwRop // raster operation code  
    );
    private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        System.Drawing.Image image = System.Drawing.Image.FromStream(this.streamToPrint);
        int x = e.MarginBounds.X;
        int y = e.MarginBounds.Y;
        int width = image.Width;
        int height = image.Height;
        if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height))
        {
            width = e.MarginBounds.Width;
            height = image.Height * e.MarginBounds.Width / image.Width;
        }
        else
        {
            height = e.MarginBounds.Height;
            width = image.Width * e.MarginBounds.Height / image.Height;
        }
        System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);
        e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
    }
    private void btnPrintDoc_Click(object sender, EventArgs e)
    {
        Graphics g1 = this.CreateGraphics();
        System.Drawing.Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1);
        Graphics g2 = Graphics.FromImage(MyImage);
        IntPtr dc1 = g1.GetHdc();
        IntPtr dc2 = g2.GetHdc();
        BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
        g1.ReleaseHdc(dc1);
        g2.ReleaseHdc(dc2);
        MyImage.Save(@"D:\PrintPage.jpg", ImageFormat.Jpeg);
        FileStream fileStream = new FileStream(@"D:\PrintPage.jpg", FileMode.Open, FileAccess.Read);
        StartPrint(fileStream, "Image");
        fileStream.Close();
    }
    public void StartPrint(Stream streamToPrint, string streamType)
    {
        this.printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);
        this.streamToPrint = streamToPrint;
        this.streamType = streamType;
        System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();
        PrintDialog1.AllowSomePages = true;
        PrintDialog1.ShowHelp = true;
        PrintDialog1.Document = printDoc;
        DialogResult result = PrintDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            printDoc.Print();
            //docToPrint.Print();  
        }
    }    

Upvotes: 2

Related Questions