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....
Am getting an empty page
ReplyDeletehttps://social.msdn.microsoft.com/Forums/vstudio/en-US/eb80fbbe-6b89-4c3d-9ede-88a2b105c714/how-to-print-windows-form-in-c-application?forum=csharpgeneral
DeleteWhere have you defined printDocument1?
ReplyDeleteprintDocument1 will come once you drag printDocument from the tool box to the form...
ReplyDeleteEmpty page.
ReplyDeleteHi Guillermo,
ReplyDeletethe 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...
..
This comment has been removed by the author.
ReplyDelete
ReplyDeleteI 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();
}
thanks OAW..may be other also face the same issue...
ReplyDeleteHi Deadlock,
ReplyDeleteI 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);
}
}
}
can you please try what OAW has suggested in comments above..
ReplyDeleteIt just appears as a blank form when I run it
ReplyDeletecan you please check code which OAW20 posted in comments on "February 2016 at 17:27"
DeleteThis comment has been removed by a blog administrator.
ReplyDeleteI'm really new to c#. Could someone tell me what the PrintPageEvent pertains to?
ReplyDeleteI 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.
Its just printing like a screenshot not thw whole form. I want to print the whole form. Can you please help me.
ReplyDeletewhat 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.
DeleteWhy Empty page
ReplyDeleteSent to printer but printer is unable to recognize it. Other documents are printed
ReplyDeleteIt 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
ReplyDeleteWhat is great respecting is dealing with instead of depending on. flat print
ReplyDeleteThanks 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