首页 > 解决方案 > 包含 RichTextBox 和图片窗口窗体的打印面板

问题描述

你好朋友我在打印Panel(PanelContain)包含RichTextBox(RichtxtHospName ,RichTxtPatDetail ,RichTxtRptBody )和图片时遇到问题,这里所有的控件都是动态创建的。当我打印panel(PanelContain)它时只打印图片而不是文本框中的文本。

定义我的控件

 RichTextBox RichtxtHospName = new RichTextBox();
        RichTextBox RichTxtPatDetail = new RichTextBox();
        RichTextBox RichTxtRptBody = new RichTextBox();
        Panel PanelContain = new Panel();

我的打印面板代码

 private void PrintPanel()
        {
            System.Drawing.Printing.PrintDocument doc = new PrintDocument();
            doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
            doc.Print();
        }
 private void doc_PrintPage(object sender, PrintPageEventArgs e)
        {
            try
            {
                Bitmap bmp = new Bitmap(PanelContain.Width, PanelContain.Height+30);
                float tgtWidthMM = 210;  //A4 paper size
                float tgtHeightMM = 297;
                float tgtWidthInches = tgtWidthMM / 25.4f;
                float tgtHeightInches = tgtHeightMM / 25.4f;
                float srcWidthPx = bmp.Width;
                float srcHeightPx = bmp.Height;
                float dpiX = srcWidthPx / tgtWidthInches;
                float dpiY = srcHeightPx / tgtHeightInches;
                bmp.SetResolution(dpiX, dpiY);
                PanelContain.DrawToBitmap(bmp, PanelContain.ClientRectangle);
                e.Graphics.PageUnit = GraphicsUnit.Millimeter;
                e.Graphics.DrawImage(bmp, 0, 0, tgtWidthMM, tgtHeightMM);
            }
            catch(Exception ex)
            {

            }
        }


 private void toolStripBtnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                PrintDocument doc = new PrintDocument();
                doc.PrintPage += this.doc_PrintPage;
                PrintDialog dlgSettings = new PrintDialog();
                dlgSettings.Document = doc;
                if (dlgSettings.ShowDialog() == DialogResult.OK)
                {
                    PrintPanel();
                }
            }
            catch (Exception ex)
            {

            }
        }

标签: c#winformsprinting

解决方案


推荐阅读