首页  ·  知识 ·  编程语言
c#调用BitBlt打印API
我是乐乐  http://laugha.ygblog.com/237358.html  .NET  编辑:dezai  图片来源:网络
操作系统】 Win9X:Yes WinNT:Yes 【声明】 BitBlt Lib "gdi32" Alias "BitBlt" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Lon
操作系统】
Win9X:Yes
WinNT:Yes

【声明】
BitBlt Lib "gdi32" Alias "BitBlt" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long

【说明】

将一幅位图从一个设备场景复制到另一个。源和目标DC相互间必须兼容

【返回值】

Long,非零表示成功,零表示失败。会设置GetLastError

【其它】

在NT环境下,如在一次世界传输中要求在源设备场景中进行剪切或旋转处理,这个函数的执行会失败
如目标和源DC的映射关系要求矩形中像素的大小必须在传输过程中改变,那么这个函数会根据需要自动伸缩、旋转、折叠、或切断,以便完成最终的传输过程

【参数表】
hDestDC -------- Long,目标设备场景

x,y ------------ Long,对目标DC中目标矩形左上角位置进行描述的那个点。用目标DC的逻辑坐标表示

nWidth,nHeight - Long,欲传输图象的宽度和高度

hSrcDC --------- Long,源设备场景。如光栅运算未指定源,则应设为0

xSrc,ySrc ------ Long,对源DC中源矩形左上角位置进行描述的那个点。用源DC的逻辑坐标表示

dwRop ---------- Long,传输过程要执行的光栅运算

例子:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace SystemWorkMaintain
{
    public class MyPrintObject
    {
        private Bitmap memoryImage = null;
        private System.Drawing.Printing.PrintDocument printDocument1 = null;

        public MyPrintObject()
        {
            printDocument1 = new System.Drawing.Printing.PrintDocument();
            printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);           
        }
       
       
        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        public static extern long StretchBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int nSrcWidth, int nSrcHeight, int dwRop);
       
        public void printPanel(System.Windows.Forms.Panel pnl)
        {
            try
            {
                Graphics mygraphics = pnl.CreateGraphics();
                Size s = pnl.Size;
                memoryImage = new Bitmap(s.Width, s.Height*2, mygraphics);
                Graphics memoryGraphics = Graphics.FromImage(memoryImage);
                IntPtr dc1 = mygraphics.GetHdc();
                IntPtr dc2 = memoryGraphics.GetHdc();
                //BitBlt(dc2,0 , 0, pnl.ClientRectangle.Width, pnl.ClientRectangle.Height, dc1, 0, 0, 13369376);
                StretchBlt(dc2, 0, 0, pnl.ClientRectangle.Width, Convert.ToInt32(pnl.ClientRectangle.Height*1.5), dc1, 0, 0, pnl.ClientRectangle.Width, pnl.ClientRectangle.Height ,13369376);
                //BitBlt(dc2, 0, pnl.ClientRectangle.Height, pnl.ClientRectangle.Width, pnl.ClientRectangle.Height, dc1, 0, 0, 13369376);
                mygraphics.ReleaseHdc(dc1);
                memoryGraphics.ReleaseHdc(dc2);
               
                PrintPreviewDialog dlg = new PrintPreviewDialog();
                dlg.Width = 800;
                dlg.Height = 600;
             //   dlg.AutoScaleMode = AutoScaleMode.Font;//ContainerControl.AutoScaleMode             
                dlg.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                dlg.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;              
                dlg.Document = printDocument1;
                if (dlg.ShowDialog() == DialogResult.OK)
                    printDocument1.Print();
            }
            catch (System.Exception ex)
            {
                //组织自助服务异常信息
                AutoServiceException.AutoServiceException myex = AutoServiceException.AutoServiceExceptionHandler.makeAutoServiceException(this.GetType().FullName, System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message, "获取登陆用户信息错误");
                //记录日志
                LogSystemInfo.LogSystemInfo.WriteLogInfo("LogSystemInfo.ErrLogSystem", myex.LogMsg);
                throw myex;
            }
        }
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(memoryImage, 50, 100);
        }
    }
}
 
本文作者:我是乐乐 来源:http://laugha.ygblog.com/237358.html
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读