您的位置:首页 > 编程语言 > C#

C#打印panel控件中内容

2012-10-18 13:10 288 查看
[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);
        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: