1
我最近試圖在C#應用程序中打印窗體。使用窗體畫面很容易,但我卡在打印部分。我已經閱讀了很多文章和很多解決方案來打印自定義尺寸的紙張(我的意思是我的表單將打印在我爲打印機指定的紙張上),但是遺憾的是沒有任何工作。無法讓打印機在C#Windows窗體項目中打印自定義大小的紙張
下面是我在我的項目迄今:
//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();
}