c# - 包含 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)
{
}
}
解决方案
推荐阅读
- android - 如何使用分页库在回收站视图中添加日期分隔符?
- javascript - 如何将道具传递给 vue.js 中的另一个组件
- python - h5py - 将数据集中的 dtype 从 int/float 转换为 bool?
- npm - 本地包的 npm 卸载抛出错误“saveError ENOENT:没有这样的文件或目录,打开'/Users/vikramanna/package.json'”
- python - Ubuntu - 登录后如何运行 python 脚本?
- ios - 使用 AKSampleDescriptor
- python - 根据条件保留熊猫数据框的上 n 行
- python - 如何不加区分地仅使用 python 内置函数(来自两个列表)创建字典?
- php - MYSQL 匹配 PHP 中的查询
- java - 使用鼠标单击绘制矩形