I’ve been recently trying to print a form in a C# application.Taking the form picture was easy, but I’m stuck in printing section. I’ve read so many articles and so many solutions to get a custom sized paper printed (I mean my form to be printed on a paper that I specify for the printer), but sadly none worked.
Here is what I have in my project so far :
//my method for taking a picture form the form , and then printing it .
void PrintImage(object o, PrintPageEventArgs e)
{
int x = SystemInformation.WorkingArea.X;
int y = SystemInformation.WorkingArea.Y;
int width = this.Width;
int height = this.Height;
Rectangle bounds = new Rectangle(x, y, width, height);
Bitmap img = new Bitmap(width, height);
// e.PageSettings.PaperSize = new PaperSize("62mm", 244, (int)Size.Width) { RawKind = 259 };
this.DrawToBitmap(img, bounds);
Point p = new Point(0, 0);
e.Graphics.DrawImage(img, p);
}
//getting the list of installed printers .
private void comboBox1_Click(object sender, EventArgs e)
{
comboBox1.Items.Clear();
foreach (string printer in PrinterSettings.InstalledPrinters)
{
comboBox1.Items.Add(printer);
}
}
//Print Button Click event declaration
private void btnPrint_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
if (comboBox1.SelectedIndex !=-1)
{
pd.PrinterSettings.PrinterName = comboBox1.SelectedItem.ToString();
}
pd.DefaultPageSettings.PaperSize = new PaperSize("CardSize", 50, 50);
pd.PrintPage += new PrintPageEventHandler(PrintImage);
pd.Print();
}
ok i found it out :
for making a custom paper size , one should just simply do :