Friday 12 April 2013

Print Windows form in c#


Print Windows form in c#, just add a printDocument from the toolbox by dragging to the form ..i wrote the code for printing the form on button click.... 






using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;

namespace Print_form1
{

   
    public partial class Form1 : Form
    {

            public Form1()
        {
            InitializeComponent();
         
        }


        private void btn_print_Click(object sender, EventArgs e)
        {
            CaptureScreen();
            printDocument1.Print();
            printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
        }

        Bitmap memoryImage;

        private void CaptureScreen()
        {
            Graphics myGraphics = this.CreateGraphics();
            Size s = this.Size;
            memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
            Graphics memoryGraphics = Graphics.FromImage(memoryImage);
            memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
        }


        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {

        }

        private void printDocument1_PrintPage_1(object sender, PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(memoryImage, 0, 0);
        }
    }
}



hope this will help you if you are facing issues just put comment on it....


22 comments:

  1. Replies
    1. https://social.msdn.microsoft.com/Forums/vstudio/en-US/eb80fbbe-6b89-4c3d-9ede-88a2b105c714/how-to-print-windows-form-in-c-application?forum=csharpgeneral

      Delete
  2. Where have you defined printDocument1?

    ReplyDelete
  3. printDocument1 will come once you drag printDocument from the tool box to the form...

    ReplyDelete
  4. Hi Guillermo,
    the code posted here will print the same form whatever is displayed , in paper,..
    can you please tell exactly what issue you are facing , as today i tested this code again, and its working fine for me...

    ..

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete

  6. I changed the order as below. Now it works fine.

    private void btn_print_Click(object sender, EventArgs e)
    {
    CaptureScreen();
    printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
    printDocument1.Print();

    }

    ReplyDelete
  7. thanks OAW..may be other also face the same issue...

    ReplyDelete
  8. Hi Deadlock,

    I was using the code below and still was unable to print the page. The printed out page just kept coming out as blank.

    Any ideas?

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing.Printing;


    namespace CX_Print
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }
    Bitmap memoryImage;

    private void CaptureScreen()
    {
    Graphics myGraphics = this.CreateGraphics();
    Size s = this.Size;
    memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
    Graphics memoryGraphics = Graphics.FromImage(memoryImage);
    memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
    }
    private void button1_Click(object sender, EventArgs e)
    {
    CaptureScreen();
    printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
    printDocument1.Print();

    }

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {

    }

    private void printDocument1_PrintPage_1(object sender, PrintPageEventArgs e)
    {
    e.Graphics.DrawImage(memoryImage, 0, 0);
    }
    }
    }

    ReplyDelete
  9. can you please try what OAW has suggested in comments above..

    ReplyDelete
  10. It just appears as a blank form when I run it

    ReplyDelete
    Replies
    1. can you please check code which OAW20 posted in comments on "February 2016 at 17:27"

      Delete
  11. This comment has been removed by a blog administrator.

    ReplyDelete
  12. I'm really new to c#. Could someone tell me what the PrintPageEvent pertains to?

    I didn't plan on using a print preview page, but I will if I have to. Also... I I don't know how to set that part up.

    ReplyDelete
  13. Its just printing like a screenshot not thw whole form. I want to print the whole form. Can you please help me.

    ReplyDelete
    Replies
    1. what do you mean by whole form, it will print the current screen form. as code is not generic one, you need to modify as per your requirements.

      Delete
  14. Sent to printer but printer is unable to recognize it. Other documents are printed

    ReplyDelete
  15. It was wondering if I could use this write-up on my other website, I will link it back to your website though.Great Thanks. which 3d printer to buy

    ReplyDelete
  16. What is great respecting is dealing with instead of depending on. flat print

    ReplyDelete
  17. Thanks for sharing this information. I really like your blog post very much. You have really shared a informative and interesting blog post with people.. cinema near me

    ReplyDelete