Print Windows form in c#

I am trying to print a form using this code:

private void btnPrint_Click(object sender, EventArgs e)
        Graphics g1 = this.CreateGraphics();
        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);
        MyImage.Save(@"c:\PrintPage.jpg", ImageFormat.Jpeg);
        FileStream fileStream = new FileStream(@"c:\PrintPage.jpg", FileMode.Open, FileAccess.Read);
        StartPrint(fileStream, "Image");
        if (System.IO.File.Exists(@"c:\PrintPage.jpg"))

But is gives me an error at: MyImage.Save.

The error:

ExternalException was Unhandled: A generic error occurred in GDI+.

Can someone give me a fix for this problem,and explain, why I'am getting this error?

Thanks in Advance!

    private void btn_print_Click(object sender, EventArgs e)
        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);

Add a print_document by dragging it from the toolbox to form. Execute this code, it will work fine.

Introduction has a PrintForm method but C# does not have inbuilt method for printing a Windows Form. The following procedure enables us to print a windows form at runtime in The base concept involves the capture of the screen image of a Form in jpeg format during runtime and printing the same on a event like Print button click.


using System.Drawing.Printing;

and in your code, add:

PrintDocument printDocument1 = new PrintDocument();

More info here

In the following code example, the event handler is used to print the "testPage.txt" file in the same font used on the form. void printDocument1_PrintPage(object sender, PrintPageEventArgs e) { int charactersOnPage = 0; int linesPerPage = 0; // Sets the value of charactersOnPage to the number of characters // of stringToPrint that will fit within the bounds of the page.

I had the same task and I have created a class (like printable form). All you have to do is to inherit your Form from that class and call PrintForm(); method. Here is the class:

public class CustomForm : Form
    protected Bitmap _prnImage;
    protected PrintPreviewDialog _prnpreviewdlg = new PrintPreviewDialog();
    protected PrintDocument _printdoc = new PrintDocument();

    public CustomForm()
        _printdoc.PrintPage += new PrintPageEventHandler(printdoc_PrintPage);

    protected void PrintForm()
        Form form = this;
        _prnImage = new Bitmap(form.Width, form.Height);
        form.DrawToBitmap(_prnImage, new Rectangle(0, 0, form.Width, form.Height));
        _printdoc.DefaultPageSettings.Landscape = true;
        _printdoc.DefaultPageSettings.Margins = new Margins(10, 10, 10, 10);
        _prnpreviewdlg.Document = _printdoc;
    protected void printdoc_PrintPage(object sender, PrintPageEventArgs e)
        Rectangle pagearea = e.PageBounds;
        e.Graphics.DrawImage(_prnImage, e.MarginBounds);

    protected override void OnPaint(PaintEventArgs e)
        if (_prnImage != null)
            e.Graphics.DrawImage(_prnImage, 0, 0);

I understand that interface is a more academic and correct way to do that but in my particular case that solution satisfied me.

It should have different (by perhaps similar) presentations for the form-based UI, maybe some other UI, printed version and maybe more. The usual way of printing, especially in the System.Windows.Forms applications is using the class System.Drawing.Printing.PrintDocument. Please see:

Windows Forms General https: I am trying to figure out the method to print a form created in VS2008 using C#. My form generates a report on an inventory maintenance.

I am trying to figure out the method to print a form created in VS2008 using C#. My form generates a report on an inventory maintenance. I have tried all the available options online but I have not been successful in searching.

how do you intend to print the form as text file? How do you convert the graphics like labels, buttons and other controls into text? what you ask is possible and you can control every aspect of the printed content in both ways graphic rendering or text only, have a look here as starting point: Windows Forms Print Support

  • I think you must Save before ReleaseHdc.
  • I think this is more simple
  • Your answer need proper explanation and formatting.