码迷,mamicode.com
首页 > Windows程序 > 详细

WinForm 调用 PrintDocument

时间:2016-11-10 19:35:30      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:cli   inbound   pre   ogre   span   splay   res   new   dia   

 

使用WinForm 打印 Devexpress BarCodeControl 二维码

        /// <summary>
        /// Handles the ItemClick event of the barButtonItem2 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="DevExpress.XtraBars.ItemClickEventArgs"/> instance containing the event data.</param>
        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            printDialog1.Document = printDocument1;
            printPreviewControl1.Document = printDocument1;
            printDocument1.PrintPage += PrintDocument1_PrintPage;
            if (printDialog1.ShowDialog() == DialogResult.OK)
            {
               
                printDocument1.Print();
                
            }

        }

        /// <summary>
        /// Prints to graphics.
        /// </summary>
        /// <param name="graphics">The graphics.</param>
        /// <param name="bounds">The bounds.</param>
        public void PrintToGraphics(Graphics graphics, Rectangle bounds)
        {
            Bitmap bitmap = new Bitmap(barCodeControl1.Width, barCodeControl1.Height);
            barCodeControl1.DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
            Rectangle target = new Rectangle(0, 0, bounds.Width, bounds.Height);
            double xScale = (double)bitmap.Width / bounds.Width;
            double yScale = (double)bitmap.Height / bounds.Height;
            if (xScale < yScale)
                target.Width = (int)(xScale * target.Width / yScale);
            else
                target.Height = (int)(yScale * target.Height / xScale);
            graphics.PageUnit = GraphicsUnit.Display;
            graphics.DrawImage(bitmap, target);
        }


        /// <summary>
        /// Handles the PrintPage event of the PrintDocument1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Drawing.Printing.PrintPageEventArgs"/> instance containing the event data.</param>
        private void PrintDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            PrintToGraphics(e.Graphics, e.MarginBounds);
        }

技术分享

 

WinForm 调用 PrintDocument

标签:cli   inbound   pre   ogre   span   splay   res   new   dia   

原文地址:http://www.cnblogs.com/w2011/p/6051663.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!