[英]Print Windows form in c#
我正在嘗試使用以下代碼打印表單:
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);
g1.ReleaseHdc(dc1);
g2.ReleaseHdc(dc2);
MyImage.Save(@"c:\PrintPage.jpg", ImageFormat.Jpeg);
FileStream fileStream = new FileStream(@"c:\PrintPage.jpg", FileMode.Open, FileAccess.Read);
StartPrint(fileStream, "Image");
fileStream.Close();
if (System.IO.File.Exists(@"c:\PrintPage.jpg"))
{
System.IO.File.Delete(@"c:\PrintPage.jpg");
}
}
但它給了我一個錯誤:MyImage.Save。
錯誤:
ExternalException 未處理:GDI+ 中發生一般錯誤。
有人可以解決這個問題,並解釋一下,為什么我會收到這個錯誤?
提前致謝!
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);
}
通過將其從工具箱拖動到表單來添加一個 print_document。 執行此代碼,它將正常工作。
http://csharpprobsandsoln.blogspot.in/2013/04/print-windows-form-in-c.html
添加:
using System.Drawing.Printing;
並在您的代碼中添加:
PrintDocument printDocument1 = new PrintDocument();
更多信息在這里
我有同樣的任務,我創建了一個類(如可打印表單)。 你所要做的就是從那個類繼承你的 Form 並調用 PrintForm(); 方法。 這是課程:
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;
_prnpreviewdlg.ShowDialog();
}
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);
base.OnPaint(e);
}
}
}
我知道接口是一種更加學術和正確的方式來做到這一點,但在我的特殊情況下,該解決方案讓我滿意。
添加以下必需的名稱空間:
using System.Drawing.Printing;
然后在您的代碼中添加:
PrintDocument printDocument1 = new PrintDocument();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.