C#打印panel控件中内容

        
  
       [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]//应用API函数
        private static extern bool BitBlt(
        IntPtr hdcDest, // 目标设备的句柄  
       int nXDest, // 目标对象的左上角的X坐标  
       int nYDest, // 目标对象的左上角的X坐标  
       int nWidth, // 目标对象的矩形的宽度  
       int nHeight, // 目标对象的矩形的长度  
       IntPtr hdcSrc, // 源设备的句柄  
       int nXSrc, // 源对象的左上角的X坐标  
       int nYSrc, // 源对象的左上角的X坐标  
       System.Int32 dwRop // 光栅的操作值  
       );
       Image memoryImage; //统计报表画图
 
 
       //打印按钮事件
       private void button_print_Click(object sender, EventArgs e)
        {

            Graphics graphic = panel_Total.CreateGraphics();
            Size s = panel_Total.Size;
            Bitmap memImage = new Bitmap(s.Width, s.Height, graphic);
            Graphics memGraphic = Graphics.FromImage(memImage);
            IntPtr dc1 = graphic.GetHdc();
            IntPtr dc2 = memGraphic.GetHdc();
            BitBlt(dc2, 0, 0, panelReports.ClientRectangle.Width, panelReports.ClientRectangle.Height,
            dc1, 0, 0, 13369376);

            //Clone the bitmap so we can dispose it. 
            memoryImage = (Bitmap)memImage.Clone();
            graphic.ReleaseHdc(dc1);
            memGraphic.ReleaseHdc(dc2);
            graphic.Dispose();
            memGraphic.Dispose();
            memImage.Dispose();


            PrintPreviewDialog dlg = new PrintPreviewDialog();
            //dlg.Width = 1800;
            //dlg.Height = 1600;
            //dlg.Document = printDocument1;
            //if (dlg.ShowDialog() == DialogResult.OK)
                printDocument1.Print(); 

        }

        //printDocument 事件
        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
             e.Graphics.DrawImage(memoryImage, -10, 100);
        }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值