Print Windows form in c#

后端 未结 3 1333
醉酒成梦
醉酒成梦 2021-01-16 01:17

I am trying to print a form using this code:

private void btnPrint_Click(object sender, EventArgs e)
    {
        Graphics g1 = this.CreateGraphics();
              


        
相关标签:
3条回答
  • 2021-01-16 01:51

    Add:

    using System.Drawing.Printing;
    

    and in your code, add:

    PrintDocument printDocument1 = new PrintDocument();
    

    More info here

    0 讨论(0)
  • 2021-01-16 02:04

    I had the same task and I have created a class (like printable form). All you have to do is to inherit your Form from that class and call PrintForm(); method. Here is the class:

    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);
            }
        }
    }
    

    I understand that interface is a more academic and correct way to do that but in my particular case that solution satisfied me.

    0 讨论(0)
  • 2021-01-16 02:13
        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);
        }
    

    Add a print_document by dragging it from the toolbox to form. Execute this code, it will work fine.

    http://csharpprobsandsoln.blogspot.in/2013/04/print-windows-form-in-c.html

    0 讨论(0)
提交回复
热议问题