1

我正在尝试使用以下代码打印表单:

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+ 中发生一般错误。

有人可以给我解决这个问题,并解释为什么我会收到这个错误吗?

提前致谢!

4

3 回答 3

3
    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

于 2013-04-12T13:44:27.893 回答
1

添加:

using System.Drawing.Printing;

并在您的代码中添加:

PrintDocument printDocument1 = new PrintDocument();

更多信息在这里

于 2019-10-30T17:59:31.200 回答
1

我有同样的任务,我创建了一个类(如可打印表单)。您所要做的就是从该类继承您的 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);
        }
    }
}

我知道界面是一种更加学术和正确的方法,但在我的特殊情况下,该解决方案令我满意。

于 2020-01-19T21:09:50.687 回答