日期:2014-05-17 浏览次数:22084 次
private void button1_Click(object sender, EventArgs e) { PrintDocument printDocument = new PrintDocument(); printDocument.DocumentName = "测试文档";//设置完后可在打印对话框及队列中显示(默认显示document) //打印输出(过程) printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage); PrintPreviewDialog ppd = new PrintPreviewDialog(); ppd.Document = printDocument; try {//显示打印预览窗口 ppd.WindowState = FormWindowState.Maximized; ppd.ShowDialog(); } catch (Exception excep) { //显示打印出错消息 MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error); } } Bitmap imageDraw() { Bitmap bitmap = new Bitmap(50, 50); SolidBrush drawBrush = new SolidBrush(Color.Black); StringFormat drawFormat = new StringFormat(); using (Graphics g = Graphics.FromImage(bitmap)) { g.DrawString("小字体测试", new Font("宋体", 6), drawBrush, 2, 2, drawFormat); } return bitmap; } void printDocument_PrintPage(object sender, PrintPageEventArgs e) { Graphics g = e.Graphics; // 指定高质量的双三次插值法。执行预筛选以确保高质量的收缩。此模式可产生质量最高的转换图像。 g.InterpolationMode = InterpolationMode.HighQualityBicubic; // 指定高质量、低速度呈现。 g.SmoothingMode = SmoothingMode.HighQuality; Bitmap image = imageDraw(); g.DrawImage(image, new Rectangle(100, 100, image.Width, image.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel); SolidBrush drawBrush = new SolidBrush(Color.Black); StringFormat drawFormat = new StringFormat(); g.DrawString("直接刷字测试", new Font("宋体", 6), drawBrush, 200, 200, drawFormat); e.HasMorePages = false; }